只有在有结果时才显示标题

时间:2015-08-11 作者:vlovsky

目前,我有以下代码来显示相关帖子:

<?php

$cat_ids = array();
$postID = get_the_ID();
$categories = get_the_terms($postID, \'article-category\'); //this will give all the terms

foreach($categories as $cat) {
    $cat_ids[] = $cat->term_id; //you may put some condition and consider only some specific terms as per your requirement
}

$args = array(
        \'post_type\' => \'article\', 
        \'posts_per_page\' => 5,
        \'tax_query\' => array(array( 
        \'taxonomy\' => \'article-category\',
        \'field\' => \'id\',
        \'terms\' => $cat_ids //ids of specified terms
        )
      )
   );

$exclude = get_the_ID();
$your_query = new WP_Query( $args );

echo \'<div class="posts-group">\';
echo \'<h2>More blog posts</h2>\';
while( $your_query->have_posts() ) : $your_query->the_post();
    if( $exclude != get_the_ID() ) {
        echo \'
        <article class="post home-post">
            <a href="\' . get_permalink() . \'">
                <div class="post-thumbnail-img">
                    \' . get_the_post_thumbnail($_post->ID, "small-thumbnail") . \'
                </div>
                <h2>\' . get_the_title() . \'</h2>
            </a>
        </article>\';
    }
endwhile;
echo \'</div><br>\';

?>
现在echo \'<h2>More blog posts</h2>\'; 应该只在我们有相关文章时显示,现在每次都会显示。

提前谢谢。

1 个回复
最合适的回答,由SO网友:jzatt 整理而成

可以在循环之前进行条件检查,以查看查询是否有帖子。如果有帖子要显示,则只显示标题和div。

$your_query = new WP_Query( $args );
if( $your_query->have_posts() ) : // Add this line here to check if the query has posts.
    echo \'<div class="posts-group">\';
    echo \'<h2>More blog posts</h2>\';
    while( $your_query->have_posts() ) : $your_query->the_post();
还要确保在循环结束后关闭新的IF语句:

    endwhile;
    echo \'</div><br>\';
endif; // Add this line to close the IF-statement.

Update:

在查询中排除当前帖子以使其生效。否则,查询仍包含当前帖子。代码如下:

$args = array(
    \'post__not_in\' => array( get_the_ID() ), //Add this line to exclude the current post.
    \'post_type\' => \'blog\', 
    \'posts_per_page\' => 5,
    \'tax_query\' => array(
        array( 
            \'taxonomy\' => \'blog-athlete\',
            \'field\' => \'id\',
            \'terms\' => $cat_ids 
        )
    )
);

$relatedquery = new WP_Query( $args );

if( $relatedquery->have_posts() ) : 
    echo \'<div class="posts-group">\';
    echo \'<h2>More blog posts:</h2>\';
    while( $relatedquery->have_posts() ) : $relatedquery->the_post();
        //Removed the IF here
        echo \'
            <article class="post home-post">
                <a href="\' . get_permalink() . \'">
                    <div class="post-thumbnail-img">
                        \' . get_the_post_thumbnail($_post->ID, "small-thumbnail") . \'
                    </div>
                    <h2>\' . get_the_title() . \'</h2>
                </a>
        </article>\';
        // Removed the closing of the IF here
    endwhile;
    echo \'</div><br>\';
endif;

结束

相关推荐

使用PHP脚本从多个WordPress站点检索数据

我想运行一个PHP脚本,迭代在特定文件夹中找到的WordPress站点,并使用get\\u plugin函数读取每个站点的插件信息在下面的代码中,我尝试在WP文件夹中要求API函数,并使用它来检索数据。然而,问题是多重“需求”。示例代码:function get_sites_plugins() { foreach($this->all_sites as $site_folder) { $plugins = get_site_plugins($site_fold