douba4933 2018-06-06 12:08
浏览 64
已采纳

Wordpress Post Meta仅适用于循环中的一个帖子

Only the first post I created is providing the url in this loop, all followings posts I create don't show the url as existing.

global $post;
$posts = get_posts(array(
    'post_type'   => 'logos',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'fields' => 'ids'
    )
);
echo "<div class='ssslider-{$slider['id']}'>";
foreach($posts as $p){

    $company_url = get_post_meta($p,"company_url",true);
    $title = get_the_title($p);
    $thumb_id = get_post_thumbnail_id($p);
    $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
    $thumb_url = $thumb_url_array[0];

    echo "<div style='margin:0px 40px;overflow:hidden;'><a href='{$company_url}' target='_blank'><img style='height:100px;' src='{$thumb_url}' alt='{$title}'/></a></div>";
}

echo "</div>";
  • 写回答

1条回答 默认 最新

  • douhensheng1131 2018-06-06 12:36
    关注

    While it's more common to use WP_Query for this, get_posts() should work just fine.

    It looks like the issue is in your use of get_post_meta().

    The first argument is expected to be the Post ID and it looks like you're passing a WP_Post Object.

    Note, get_post_thumbnail_id() and get_the_title() should take the ID as well, but are stated to also accept a WP_Post Object.

    $posts = get_posts([
        'post_type'      => 'logos',
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'fields'         => 'ids'
    ]);
    
    echo "<div class='ssslider-{$slider['id']}'>";
        foreach( $posts as $p ){
            $company_url = get_post_meta( $p->ID, 'company_url', true );
            $title       = get_the_title( $p->ID );
            $thumb_id    = get_post_thumbnail_id( $p->ID );
            $thumb_array = wp_get_attachment_image_src( $thumb_id, 'thumbnail-size', true );
            $thumb_url   = $thumb_array[0];
    
            echo "<div style='margin:0px 40px;overflow:hidden;'><a href='{$company_url}' target='_blank'><img style='height:100px;' src='{$thumb_url}' alt='{$title}'/></a></div>";
        }
    echo "</div>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部