


Loops are a combination of object oriented and global behaviour. There are other Conditional Tags that can be used to control the output depending on whether or not a particular condition is true with respect to the requested page. Note: If you use this example for your main page, you should use a different Template for your Category archives otherwise, WordPress will exclude all posts in Category 3 and Category 8, even when viewing that Category Archive! However, if you want to use the same template file, you can avoid this by using the is_home() tag to ensure that posts from Category 3 and Category 8 will only be excluded from the main page: Have_posts() ) : while ( $query->have_posts() ) : $query->the_post() ?> The example is different than the example above in that it makes a change to the query itself. In this case, posts from Categories 3 and 8 are excluded.
#Php for loop how to#
This example demonstrates how to hide a specific Category or Categories from being displayed.
#Php for loop code#
You can start and stop blocks of PHP code to intersperse HTML code within if and else statements, as shown in the above example. And, PHP code (even things as simple as curly braces: } ) must be inside the tags. Note: All HTML code must be outside the tags. " rel="bookmark" title="Permanent Link to "> Read the comments carefully to see what each part of the code is doing.

To accomplish this, the in_category() Template Tag is used. It also allows posts in a category with Category ID '3' to be styled differently. This example displays each post with its Title (which is used as a link to the Post's ), Categories, and Content. Loop Examples Style Posts From Some Category Differently This is using PHP's alternative syntax for control structures, and could also be expressed as: If you are using The Loop inside your own design (and your own design is not a template), set WP_USE_THEMES to false: The Loop should be placed in the Theme's index.php and in any other Templates used to display post information.īe sure to include the call for the header template at the top of your Theme's templates. You can display other information about each post using the appropriate Template Tags or (for advanced users) by accessing the $post variable, which is set with the current post's information while The Loop is running.įor a beginner's look at The Loop, see The Loop in Action. For example, The Loop displays the following information by default for each post:

When WordPress documentation says "This tag must be within The Loop", such as for specific Template Tags or plugins, the tag will be repeated for each post. Any HTML or PHP code in the Loop will be processed on each post. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. The Loop is PHP code used by WordPress to display posts.
