Sidebar, Wordpress

Show Recent Posts from a particular term in Custom Taxonomy

2 45

Since the introduction of Custom taxonomies and custom post types, Many WordPress users started using them. I am one of those developers who have used custom taxonomies in my recent website called Windows7themer. In windows 7 themer, I have used custom taxonomy called “Windows 7 utilities”. Windows 7 utilities contains terms such as Themes, Screensavers, rainmeter themes, visual styles, etc. So i wanted to show recent posts from each of those terms in sidebar. So i started working on showing recent posts from particular term in a custom taxonomy. Now i have even applied it for smashingtips as well as i created a custom taxonomy called hacks for grouping WordPress hacks. Now let us see the code and its implementation.


Know the Code
PHP Snippet

<?php

$args = array( 'taxonomy'=>'Hacks','term'=>'theme-functions','posts_per_page'=>5 );

$action_films = new WP_Query( $args );

while( $action_films->have_posts() ) : $action_films->the_post();

?>

<!-- recent posts list style goes here-->


<?php endwhile; ?>      

In the above PHP snippet, you need to edit two lines.


$args = array( 'taxonomy'=>'utilities','term'=>'themes','posts_per_page'=>5 );

In the above line, ‘Utilities’ represents my custom taxonomy name. Remember this is case sensitive, so you must type the custom taxonomy name same as you typed while creating.


'term'=>'theme-functions'

I am trying to show posts from the term ‘themes’ in custom taxonomy called “Utilities”. So you must type the name as you see in the term slug.


'posts_per_page'=>5

This is used to control the number of posts you want to show under that particular term in the custom taxonomy.


<!-- recent posts list style goes here-->

It is the area in which you need to add the code to display the recent posts. If you just want to display list of recent posts without any thumbnail, then you can use the following code in that area.


<li><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>

If you want to show recent posts along with thumbnail and other data such as number of comments and date, then use the following code.


<li>

<?php echo get_the_post_thumbnail($id, array(50,50) );?>

<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">

<?php the_title(); ?>
</a>

<span class="meta"><?php the_date(); ?> | <?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); 
?>
</span>

</li>

Here I am using the default get_the_post_thumbnail() function to get the thumbnail. But the theme developers may use different ways to get the thumbnails. For example, some still use the old “Custom fields” to get the thumbnail image. So please add corresponding code to get your thumbnail, anyhow for most of the newer themes get_the_post_thumbnail() will work fine.


<?php echo get_the_post_thumbnail($id, array(50,50) );?>

In the above line, the numbers within the array 50,50 represents the image height and width. Try changing it to suit your sidebar design.

Implementation of Code

Let’s see the final code to implement in your blog.

Recent Posts without Thumbnail


<?php

$args = array( 'taxonomy'=>'Hacks','term'=>'theme-functions','posts_per_page'=>5 );

$action_films = new WP_Query( $args );

while( $action_films->have_posts() ) : $action_films->the_post();

?>

<li><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>


<?php endwhile; ?>      

Recent Posts with Thumbnail


<?php

$args = array( 'taxonomy'=>'Hacks','term'=>'theme-functions','posts_per_page'=>5 );

$action_films = new WP_Query( $args );

while( $action_films->have_posts() ) : $action_films->the_post();

?>


<li>
<?php echo get_the_post_thumbnail($id, array(50,50) );?>

<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<span class="meta"><?php the_date(); ?> | <?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); ?></span>
</li>

<?php endwhile; ?>      

Conclusion

You need to place those codes in your template to show the recent posts from a particular term in custom taxonomy of wordpress. I guess that this article will be useful to you all. As we all know, in WordPress we can do same thing in different ways. If you have any other alternative please let me know. I will add your trick with credit to your blog.

About the author / 

admin

Related Posts

Popular

Editor’s Pick

  • Optimization and acceleration of Windows : Disable Superfetch

    If you are a gamer or an advanced user that need’s a lot of memory on your windows 7  you can get it without having to add ram to your pc by just 2 or 3 click , Disable the superfetch If you are one of the people that use the ready boost just close…

  • How to add multiple Authors in Blogger

    The technique of having Multiple authors to manage a Blog is getting popular these days. Though this feature was already available in wordpress , recently Blogger too has enabled this feature. So now Blogger Users can have upto 100 multiple Authors or co-Authors for their blogs. So this new Team Blogging feature has enabled multiple…

  • Exploring the Wonders of Gigapixel Photography: How to Achieve Stunning Detail and Clarity in Your Images

    Gigapixel photography is a technique that captures images with incredibly high resolution and detail, allowing for stunningly clear and vibrant pictures that are perfect for printing or displaying on large screens. In this article, we’ll explore some tips and techniques you can use to achieve amazing results with gigapixel photography. Definition of gigapixel photography Gigapixel…

  • Implementing SOLID Principles in Your React Frontend Development

    Learn how to improve the maintainability and scalability of your React frontend applications by implementing the SOLID principles. From Single Responsibility to Dependency Inversion, this article covers practical examples and best practices for writing cleaner, more modular code in your React codebase. Join us on this journey towards better frontend development with Implementing SOLID Principles in Your React Frontend Development.

  • Ultimate List of Portable Applications for USB Stick

    Create Portable Applications CEEDO Portable Application creator Ceedo is something similar to Mojopac and will let you port your operating system to your USB drive. Ceedo uses virtualisation technology and algorithms to acheive this. Ceedo is also not free and you need license to use both Ceedo Personal and Ceedo Enterprise. Download Here   Prayaya…