在循环中跳过开机自检并稍后混合

时间:2014-10-04 作者:sinini

在博客中,我尽量避免连续显示同一类别的两篇文章。因此,如果最近的两篇文章来自第1类,我想跳过第二篇,先选第3篇(如果它也不是来自第21类),然后在显示第4篇之前混合跳过的文章。

所以我得到的是

while (have_posts()) : the_post();

    //get category of post
    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID; 

    //set flag to know, when there was a post form category 1
    if ($flag == true && ($category_id == 1)) {

        //store post to use later
        ....

        continue;
    }

    if ($category_id == 1)
        $flag = true;
    else
        $flag = false;

}
有人有主意吗?

3 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

这样的方法怎么样:

$first_category_args = array(
    // category id
    \'cat\'                 => 123,
    // get all from cat
    \'posts_per_page\'       => -1,
    // don\'t prepend sticky
    \'ignore_sticky_posts\' => 1,
    // only return ids
    \'fields\'              => \'ids\'
);
// array of ids
$first_category_query = new WP_Query(
    $first_category_args
);
$first_category_ids = $first_category_query->posts;

$second_category_args = array(
    // category id
    \'cat\'                 => 456,
    // get all from cat
    \'posts_per_page\'       => -1,
    // don\'t prepend sticky
    \'ignore_sticky_posts\' => 1,
    // only return ids
    \'fields\'              => \'ids\'
);
// array of ids
$second_category_query = new WP_Query(
    $second_category_args
);
$second_category_ids = $second_category_query->posts;

// count    
$first_category_ids_count  = count( $first_category_ids );
$second_category_ids_count = count( $second_category_ids );

// set count to the greater one    
if ( $first_category_ids_count > $second_category_ids_count ) {
    $count = $first_category_ids_count;
} else {
    $count = $second_category_ids_count;
}

$combined_alternating_category_ids = array();

// create combined alternating array of ids
for ( $c = 0; $c < $count; $c++ ) {
    if ( isset( $first_category_ids[ $c ] ) ) {
        $combined_alternating_category_ids[] = $first_category_ids[ $c ];
    }
    if ( isset( $second_category_ids[ $c ] ) ) {
        $combined_alternating_category_ids[] = $second_category_ids[ $c ];
    }
}

// now do a query with the combined result
$combined_category_args = array(
    // get post by created alternating ids array
    \'post__in\' => $combined_alternating_category_ids,
    // we\'re ordering by the order the ids have in the array
    \'orderby\'  => \'post__in\'
);
$combined_query = new WP_Query(
    $combined_category_args
);

// proceed with a loop
if ( $combined_query->have_posts() ) {
    while ( $combined_query->have_posts() ) {
        $combined_query->the_post();
        // show post code
    }
} else {
    // no post code
}

wp_reset_postdata();
正如一个想法一样,没有测试这个,对它进行了快速测试,确实如预期的那样工作。

要更改主查询,可以执行以下操作:

function wpse163419_alternate_order_array() {
    $first_category_args = array(
        // category id
        \'cat\'                 => 1,
        // get all from cat
        \'posts_per_page\'       => -1,
        // only return ids
        \'fields\'              => \'ids\'
    );
    // array of ids
    $first_category_query = new WP_Query(
        $first_category_args
    );
    $first_category_ids = $first_category_query->posts;

    $second_category_args = array(
        // category id
        \'cat\'                 => 3,
        // get all from cat
        \'posts_per_page\'       => -1,
        // only return ids
        \'fields\'              => \'ids\'
    );
    // array of ids
    $second_category_query = new WP_Query(
        $second_category_args
    );
    $second_category_ids = $second_category_query->posts;

    // count    
    $first_category_ids_count  = count( $first_category_ids );
    $second_category_ids_count = count( $second_category_ids );

    // set count to the greater one    
    if ( $first_category_ids_count > $second_category_ids_count ) {
        $count = $first_category_ids_count;
    } else {
        $count = $second_category_ids_count;
    }

    $combined_alternating_category_ids = array();

    // create combined alternating array of ids
    for ( $c = 0; $c < $count; $c++ ) {
        if ( isset( $first_category_ids[ $c ] ) ) {
            $combined_alternating_category_ids[] = $first_category_ids[ $c ];
        }
        if ( isset( $second_category_ids[ $c ] ) ) {
            $combined_alternating_category_ids[] = $second_category_ids[ $c ];
        }
    }

    return $combined_alternating_category_ids;
}

add_action( \'pre_get_posts\', \'wpse163419_cat_alternate_order\' );
function wpse163419_cat_alternate_order( $query ) {
    // avoid infinite loop
    remove_action( \'pre_get_posts\', __FUNCTION__ );
    // get ids
    $combined_alternating_category_ids = wpse163419_alternate_order_array();
    // set query parameter
    if ( ! is_admin() && $query->is_main_query() ) {
        $query->set( \'post__in\', $combined_alternating_category_ids );
        $query->set( \'orderby\', \'post__in\' );
        $query->set( \'posts_per_page\', \'-1\' );
    }
}

SO网友:jetlej

这将存储一个变量,以检查最近显示的帖子是否属于类别1。如果下一篇文章也在类别1中,它会将其添加到存储的文章数组中。

这允许您存储多个帖子,以防一行中有多个类别1帖子。

最后,如果您已经到达循环中的最后一项,它会吐出所有存储的帖子。

    $prev_cat1 = \'\';
    $count = 0;
    $stored_posts = array();

    while (have_posts()) : the_post();

        // Starting counting posts so we know if we reach the end of the loop
        $count++;

        // Check if post is in category 1
        $categories = wp_get_post_categories();
        if(in_array(\'1\', $categories)){
            $cat1 = true;
        }else{
            $cat1 = false;
        }

        //check if previous post category is the same
        if ($cat1 && $prev_cat1) {

            // Store the post ID and category in an array for later use (must use array in case there are multiple posts in a row with same category)
            $stored_posts[] = get_the_ID();

        }else{

            // YOUR POST CONTENT

            // Specify whether or not this post was in category 1
            if($cat1) $prev_cat1 = true;
            else $prev_cat1 = false;

        }

        // Check if there is a stored post AND that the previous displayed post was not in category 1
        if(!empty($stored_posts) && !$prev_cat1){

             // YOUR POST CONTENT
             // make sure to specify the post ID since you\'re not using the current post in the loop, eg. echo get_the_title($postID);

            // Remove that post from the stored posts array
            array_shift($stored_posts);

            // Specify that last printed post was in category 1
            $prev_cat1 = true;

        }

        // If this was the last post in the loop, just output the remaining stored posts
        if($count == sizeof($posts)){
            foreach($stored_posts as $post){

                // YOUR POST CONTENT (same note as above about specifying the post ID)

            }
        }
    }

SO网友:sinini

最后,我进行了两次查询,第一次是获取帖子的ID,第二次是按照我想要的排序类型返回帖子:

$flag = false;

//arguments for first query
$args = array(
        \'post_type\' => \'post\',
        \'posts_per_page\' => 500,
);

$query = new WP_Query($args);

//make first query only to get the ids
while ($query->have_posts()) : $query->the_post();

    //get category of post
    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID;          

     if (($category_id == 1)) {

        //if it is second appearence of cat1 post
        $postToSave[] = $post->ID;
        $flag = true;

    } else if ($flag == true && ($category_id != 1)) {

        //if flag is true, mixin from array
        $defaultPost[] = $post->ID;

        if (!empty($postToSave)) {
            $idSavedPosts = array_shift($postToSave);
            $defaultPost[] = $idSavedPosts;
            $flag = true;
        }

    } else if ($flag == false && ($category_id != 1)) {

        // flag is false, just add no normal array
        $defaultPost[] = $post->ID;
        $flag = false;

        if (!empty($postToSave)) {
            $idSavedPosts = array_shift($postToSave);
            $defaultPost[] = $idSavedPosts;
        }
    }

endwhile;
第二个有这样的论点:

$args2 = array(
        \'post_type\' => \'post\',
        \'posts_per_page\' => 12,
        \'paged\' => ( get_query_var(\'paged\') ? get_query_var(\'paged\') : 1),
        \'post__in\' => $defaultPost,
        \'orderby\' =>\'post__in\'
);
谢谢你的帮助!把我带到了正确的方向

结束

相关推荐

Backup blog posts only

我有一个Wordpress网站,有很多页面和博客帖子。我想创建一个新网站,但保留所有博客帖子,只有博客帖子,没有页面。我怎样才能做到这一点?当我从mysql备份数据库时,所有内容都会传递到新站点,但我只想备份博客帖子。WordPress Backups