【问题标题】:Get the specific value of the_meta() in WordPress?获取WordPress中the_meta()的具体值?
【发布时间】:2013-03-28 02:53:05
【问题描述】:

我正在竭尽全力地使用 Wordpress 中的 the_meta() 数组从循环中获取特定行,并为其分配一个变量(如程序 while 循环)。现在它只是一次拉出所有的行。此查询的结果从数据库返回 3 行。我正在尝试将 div 标签包裹在存储在 the_meta(); 中的返回行之一周围。

我尝试过分解数组并返回第一行,但它只是一次返​​回所有内容。我只想获取一行并将其放入一个变量中,以便我可以使用 CSS 对其进行样式设置。

代码如下:

$args = array( 
'post_type' => 'membersprofile', 
'posts_per_page' => 20);        

$loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();  

$newvar = explode(" ", the_meta());
echo $newvar[0];

endwhile;

任何帮助将不胜感激!

编辑:感谢 Youn 为我指明了正确的方向并为我找到了答案,问题是我使用的是仅返回整行的 the_meta()。通过使用 get_post_meta,我能够将变量分配给返回的每一行。有效的代码是:

$key_2_value = get_post_meta(get_the_ID(), 'wpcf-shortbio', true);

// check if the custom field has a value
if($key_2_value != '') {
    echo $key_2_value;
}  

希望这对其他人有帮助!

【问题讨论】:

  • 您的意思是按照codex.wordpress.org/Function_Reference/get_post_meta 中的描述使用 $var=get_post_meta() ? the_meta() 不会将元数据存储在变量中,它只是显示它
  • 哈哈@YounElan 非常感谢你为我指明了正确的方向!!!!我已经按照你说的做了,并使用了 get_post_meta。现在我可以将行拆分为变量。非常感谢您,非常感谢您的快速回复:)

标签: php wordpress meta


【解决方案1】:

尝试使用 get_post_meta() 代替元。它将返回特定页面/帖子的元值。

欲了解更多信息,请访问http://codex.wordpress.org/Function_Reference/get_post_meta

【讨论】:

  • 谢谢,我可以在我的主题单页 (content-single.php)
【解决方案2】:

你可以在循环中使用

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
   <p><?php echo post_custom( 'my_meta_key' ) ; ?></p>
<?php  endwhile; endif; ?>

【讨论】:

    猜你喜欢
    • 2015-07-24
    • 2017-05-05
    • 2023-01-31
    • 2020-08-29
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2019-05-18
    • 2015-02-03
    相关资源
    最近更新 更多