【问题标题】:Wordpress issue with if condition when using it inside LoopWordpress 在循环中使用 if 条件时出现问题
【发布时间】:2017-01-13 16:56:38
【问题描述】:

我在帖子循环中使用 if 条件来显示只有帖子有缩略图,它工作得很好但是循环计算分页栏中没有任何缩略图和分页的隐藏帖子也一直给我隐藏帖子的空白页如何我可以强制循环只计算 if 条件帖子的结果吗 这是我的代码

        <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'paged' => $paged,
    'post_type' => array('post','news','video'),
    );
    query_posts($args);
    while(have_posts()) : the_post(); ?>
    <?php if(has_post_thumbnail()) { ?>
       <?php echo get_the_title(); ?>
    <?php }else{} ?>
    <?php endwhile; ?>
    <?php wpbeginner_numeric_posts_nav(); ?>

【问题讨论】:

    标签: wordpress loops if-statement post pagination


    【解决方案1】:

    如果您不希望它们出现,最好不要返回没有缩略图的结果。

    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
        'paged' => $paged,
        'meta_key' => '_thumbnail_id', // only return results with thumbnail
        'post_type' => array('post','news','video'),
        );
        query_posts($args);
    
        while(have_posts()) : the_post(); 
    
        if(has_post_thumbnail()) { 
                echo get_the_title(); 
        }
    
        endwhile;
    
        wpbeginner_numeric_posts_nav();
      ?>
    

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多