如何先显示特色帖子,然后再为其他帖子分开循环

时间:2017-10-27 作者:Chris L

我遵循了instructions from the Codex 对于多个循环,但我有问题。

我有一个名为“特色”的类别和一个名为“加拿大2017”的标签,这是我想在页面上显示的帖子。下面是我的代码,以及我所尝试的内容的注释:

<?php 

// Attempting to show featured first: the array pulls one post from the \'Featured\' category with the relevant tag. This works correctl

$my_query = new WP_Query(array(
            \'category_name\' => \'featured\',
            \'posts_per_page\' => 1,
            \'tag_slug__in\' => get_the_title() ) );

while ( $my_query->have_posts() ) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>

<!-- Code to display featured post with relevant formatting goes here -->

<?php

// This ends the first loop

endwhile; ?>

<?php 

// The query for standard posts: this is supposed to check posts for the relevant tag. This loop worked correctly before I added the loop for featured posts

$the_query = new WP_Query(array(
            \'tag_slug__in\' => get_the_title(), 
            \'posts_per_page\' => -1 
            ) );

// The loop to display these posts according to the query

if( $the_query->have_posts() ):

    while( $the_query->have_posts() ): $the_query->the_post(); 

// This line is from the Codex, and is supposed to prevent duplication of the featured post. I\'m unsure if this is implemented correctly?

    if ( $post->ID == $do_not_duplicate ) continue; ?>

<!-- Formatting for regular posts -->

<?php

    // Ending second loop

    endwhile;

    endif;

?>
我很想得到任何帮助,使这两个循环的工作!

1 个回复
SO网友:rudtek

你这里有很多问题。首先,您要求Tag\\u slug\\uu in,但提供了一个标题,其次,Tag\\u slug\\uu in接受一个数组(我不确定它是否适用于字符串)。这两个问题都会导致问题,因为标题不是段塞,数组不是字符串。。也许就用“标签”?但如果页面标题不等于一个slug呢?那么您就有一个错误,没有错误消息来说明发生了什么。(我相信这就是这里发生的事情。)或者您有多个特色帖子?

您有2个自定义查询,但没有在其中任何一个中重置post数据。

我要求查看其余的代码,因为我不知道这是什么类型的页面,与args中的“get\\u the\\u title()”引用相关。这是存档、模板、类别或分类页面等。

下面是一个代码示例。我已经从您的两个查询中注释掉了您的tag\\u slug\\uu。我已经添加了post重置,我已经为您添加了实际返回的内容。

<?php 

// Attempting to show featured first: the array pulls one post from the \'Featured\' category with the relevant tag. This works correctly

$my_query = new WP_Query(array(
            \'category_name\' => \'featured\',
            \'posts_per_page\' => 1,
            //\'tag_slug__in\' => get_the_title() 
            ) 
            );

while ( $my_query->have_posts() ) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>

<!-- Code to display featured post with relevant formatting goes here -->
echo \'My Featured Post: \'. get_the_title().\'<br><hr><br>\';
<?php

// This ends the first loop

endwhile; 
wp_reset_postdata();?>
<?php 

// The query for standard posts: this is supposed to check posts for the relevant tag. This loop worked correctly before I added the loop for featured posts

$the_query = new WP_Query(array(
            //\'tag_slug__in\' => get_the_title(), 
            \'posts_per_page\' => -1 
            ) 
            );

// The loop to display these posts according to the query

if( $the_query->have_posts() ):

    while( $the_query->have_posts() ): $the_query->the_post(); 

// This line is from the Codex, and is supposed to prevent duplication of the featured post. I\'m unsure if this is implemented correctly?

    if ( $post->ID == $do_not_duplicate ) continue; ?>

<!-- Formatting for regular posts -->
echo \'Not a Featured Post: \'. get_the_title().\'<br>\';
<?php

    // Ending second loop

    endwhile;
wp_reset_postdata();
    endif;
?>

结束

相关推荐

Show the excerpt in a loop

我想用这个循环显示最后3篇文章。这很有效,但我不知道为什么,但摘录总是一样的。我做错了什么? <?php $args = array( \'numberposts\' => \'3\' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ ?> <div class=\"