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.
You have serious issues here
For each loop you are running two queries, one with
get_posts()
and one withquery_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 SEOFurthermore,
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 badquery_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 usingsetup_postdata( $post )
Still on the debugging part,
showposts
was dropped in favor ofposts_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
:
andendforeach
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 easyALWAY ALWAYS, VERY IMPORTANT, always reset custom queries. Use
wp_reset_postdata()
to reset the$post
global whenever you callsetup_postdata()
orthe_post()
in a custom query. Just for interest, because I have already stated to never usequery_posts
, you should usewp_reset_postdata()
to reset custom queries created withquery_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 css3The following is untested, but you can try the following