php - 显示来自 WordPress 帖子自定义字段的图像

标签 php wordpress random

我使用以下代码将随机帖子数据拉到我博客的侧边栏上。我如何添加从该帖子的自定义字段“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/

相关文章:

php - 此表单输入需要多少卫生处理?

php - Wordpress 使用 PHP 更新用户密码

php - 单独获取 Woocommerce 中每个购物车和订单商品的税率

bash - Bash 中 RANDOM 和 SRANDOM 的区别

java - 选择随机数以选择数组索引时应用程序崩溃

php - 回显已发布 ID 的行名称,用于预选的表单值

php - Laravel Query Builder 如何跳过并从最新到最旧的行?

javascript - 性能 css 规则与 jQuery .toggleClass

java.util.Random 特性

PHP:$_SESSION 变量未传递到下一个网页