Home » Wordpress » Create WP Loops Based On Category Or Tags

Create WP Loops Based On Category Or Tags

You can use this tutorial if youre building a WordPress blog or want to control specific theme sections or whatever you have on your mind :D.

You can place just one loop or place all loops depends on what you’re building. You can place them in index.php or any other page where you want to display your custom posts.

First Loop Pulling Posts From ‘wordpress’ Category

<!-- First Loop, 3 Posts from wordpress category -->
<h3>3 WordPress Posts</h3>
<ul>

<?php
// Arguments for WP_Query
$args = array(
'category_name' => 'wordpress',
'showposts' => 3 // How Many Posts To Show
);

// The New Loop Stored In $wordpress
$wordpress = new WP_Query( $args );
while ( $wordpress->have_posts() ) : $wordpress->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>

<?php
// Loop Ends
endwhile;

// Reset The Query
wp_reset_postdata();
?>
</ul>

Second Loop Pulling Posts From ‘design’ Category

<!-- Second Loop, 3 Posts From Design Category -->
<h3>3 Design Posts</h3>
<ul>

<?php
// Arguments For WP_Query
$args = array(
'category_name' => 'design',
'showposts' => 3 // How Many Posts To Show
);

// The New Loop Stored In $design
$design = new WP_Query ( $args );
while ( $design->have_posts() ) : $design->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>

<?php // Loop Ends
endwhile;

// Reset The Query
wp_reset_postdata();
?>
</ul>

Third Loop Pulling Posts From ‘themes’ Tag

<h3>Three Posts From The Tag Themes</h3>
<ul>

<?php
// Arguments for Wp_Query
$args = array(
'tag' => 'themes',
'show_posts' => 3 // How Many Posts To Show
);

// The New Loop Stored In $themes
$themes = new WP_Query( $args );
while ( $themes->have_posts() ) : $themes->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>

<?php
// Loop Ends
endwhile;

// Reset The Query
wp_reset_postdata();
?>
</ul>
Tags :
Uzabila

About Uzabila

Web Design, WordPress developer & enthusiast. Co-Founder of CreativeVerse.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

If you are not a robot - solve an example: * Time limit is exhausted. Please reload the CAPTCHA.

Scroll To Top