我使用以下代码将随机帖子数据拉到我博客的侧边栏上。我如何添加从该帖子的自定义字段“athletethumbnail”中提取的图像? :
<?php
global $wp_query;
$current_id = $wp_query->get_queried_object_id();
$my_query = new WP_Query( array(
'post_type'=>'athletes',
'posts_per_page'=>'1',
'category' => '36' ,
'orderby' =>'rand'
));
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
}
wp_reset_query();
?>
最佳答案
来自docs found here ,在 PostMeta 函数下
These functions are intended for use inside The Loop, and all return arrays.
get_post_custom()
Get all key/value data for the current post.
................
解决方案:您应该使用 get_post_custom() .
试试这个:
$custom = get_post_custom(the_ID());
echo $athletethumbnail = $custom["athletethumbnail"][0];
注意:您也应该能够在不传递 POST ID 的情况下逃脱,因为 get_post_custom 调用 get_the_id id 帖子 ID 未传递。 source here
更改后:
<?php
global $wp_query;
$current_id = $wp_query->get_queried_object_id();
$my_query = new WP_Query( array(
'post_type'=>'athletes',
'posts_per_page'=>'1',
'category' => '36' ,
'orderby' =>'rand'
));
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$custom = get_post_custom(the_ID());
echo $athletethumbnail = $custom["athletethumbnail"][0];
the_title(); ?>
<?php
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
关于php - 显示来自 WordPress 帖子自定义字段的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11181071/