WordPress的帖子元仅在循环中工作应该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (31)

只有我创建的第一篇文章是在这个循环中提供网址,创建的所有后续文章都不会将网址显示为现有网址。

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>";
提问于
用户回答回答于
$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>";

扫码关注云+社区

领取腾讯云代金券