Wordpress multiple loops and general loop

2019-07-20 17:57发布

I need creating custom multiple loops for my blog home.php in order to show 3 different content layout of defined category then proceed with the general loop excluding posts ID within the first 3 loops.

So far I made the the 3 different content layout of defined category category_name=arts:

<?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count1++; } ?>
<?php endforeach; ?>


<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count2++; } ?>
<?php endforeach; ?>


<?php query_posts('category_name=arts&showposts=1'); ?>
<?php $posts = get_posts('numberposts=1&offset=2'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == "1") { break; } else { ?>

<?php the_title(); ?>

<?php $count3++; } ?>
<?php endforeach; ?>

I did get stuck to proceed with the general loop. Any suggestions are much appreciated.

标签: php wordpress
1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-20 18:16

You have serious issues here

  • For each loop you are running two queries, one withget_posts() and one with query_posts

  • You are making use of query_posts which you should never ever use. This adds a huge overhead on your query as you rerun the main query, so you are actually running 3 queries to get 1 post for each loop. This slows your page down which costs you dearly when it comes to SEO

    Furthermore, query_posts breaks the main query object which breaks thousands of functions an plugins that relies on the main query object. You should really spent time and read this post on just how really bad query_posts is.

  • start_wp() was already depreciated in WordPress version 1.5. This means your code has bugs, and one should avoid bugs at all costs. You should really turn on debug while developing as debug will immediately throw a debugging message telling you that you are using a depreciated function. Here is an article from the codex on how to work with the debugging feature in WordPress. You should be using setup_postdata( $post )

  • Still on the debugging part, showposts was dropped in favor of posts_per_page

  • You are running a foreach loop without making sure you have posts to display. ALWAYS ALWAYS make sure you have valid values before you try to do anything with a dynamic variable. This will avoid numerous bugs should your variable return an empty value.

  • You should really work on your formatting as your code is quite hard to read when everything is packed into one line. Use proper indentation. Properly indented and formatted code does help a lot with readability and debugging. Also, drop the : and endforeach syntax. Although it is valid, it is not supported by code editors which make debugging a nightmare should your code fail. I always tell everyone to use the old style curly brackets as all code editors support them. They make debugging very easy

  • ALWAY ALWAYS, VERY IMPORTANT, always reset custom queries. Use wp_reset_postdata() to reset the $post global whenever you call setup_postdata() or the_post() in a custom query. Just for interest, because I have already stated to never use query_posts, you should use wp_reset_postdata() to reset custom queries created with query_posts

  • Lastly, you can do what you need with the specified category in only one loop, not three. Simply make use of your counters. If this is purely for styling, you can simply use the :nth child() selector in css3

The following is untested, but you can try the following

$special_cat_args = [
    'category_name' => 'art',
    'posts_per_page' => 3,
    //Add extra arguments here
];
$art_posts = get_posts( $special_cat_args );

// Setup a variable to store the post ID's so we can exclude them in the 'main' query
$ids_array = [];

// Check if we have posts before we continue
if ( $art_posts ) {
    // Start our counter
    $counter = 0;

    // Start the loop
    foreach ( $art_posts as $post ) {
        // Setup postdata, must use $post
        setup_postdata( $post );

        // Store the post id in an array to exclude in "main" query
        $ids_array[] = $post->ID;

        // Do something separate for every post
        if ( 0 == $counter ) {
            // Do something for post one
        } elseif ( 1 == $counter ) {
            // Do something for post two
        } elseif ( 2 == $counter ) {
            // Do something for post three
        }

        // Update the counter
        $counter++;
    } //endforeach $art_posts
    wp_reset_postdata(); // VERY VERY IMPORTANT
} //endif $art_posts

// Now we can do our "main" query and exclude the three posts from the special category
$args = [
    'post__not_in' => $ids_array, // Exclude the three post from previous query
    // Rest of your arguments
];
$q = get_posts( $args );
// Run your loop
查看更多
登录 后发表回答