douba4933 2018-06-06 20: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 20: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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝