List links of Posts determined by Category

– Hijack The Loop, Drop in Plugin, Call with Short Code

The Problem: I want to list just my last 5 happenings blog posts in a widget on my homepage. I, currently, have a blog page with all my posts ever and I have a widget that will list with links, my five recent posts. What I want to have is an easy way to call just a few recent blog posts of a certain category, happenings in this case, to show up as a list of links to the blog post.

The solution:

like the following post link list by category dev
***************Short Code Example in Action***********************


***********End of Short Code Example in Action********************

How it’s done

Create a plugin file named: my_cat_list_query/my_cat_list_query.php
The following code was placed into a folder, called my_cat_list_query, that is placed in the plugins folder /public_html/wp-content/plugins
*be sure to activate plugin in the Admin, once installed.
shortcode is called by placing brackets around ->
others_cat_list category_name=whatever the category slug name is
similar to this [others category=Headlines]
Here’s what was dropped into plugin file, my_cat_list_query/my_cat_list_query.php:

<?php
/*
Plugin Name: WordPress Meetup OC Demos category_name list query
Description: Some demos for Meetup
Version: 1.0
Author: mitcho tweaked by eshilling
Author URI: http://mitcho.com
*/

 add_shortcode('others_cat_list', 'get_other_cat_list_posts');
 function get_other_cat_list_posts($attributes) {
   $return = '';
   $category_name = $attributes['category_name'];
   $my_query = new WP_Query(array('category_name'=>$category_name));
   if ( $my_query->have_posts() ) {
      $return .= "<ul>";
      while ( $my_query->have_posts() ) {
	$my_query->the_post();
	$return .= "<li><a href='";
	$return .= get_permalink();
	$return .= "'>";
	$return .= get_the_title();
	$return .= "</a>";
	$return .= "</li>";
       }
/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset.
*/
	$return .= "</ul>";
     }
     return $return;
    }
	
	?>

Can switch from using the category to determine how posts will be called, by changing out category_name with different parameter
http://codex.wordpress.org/Class_Reference/WP_Query

Leave a Reply

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

*

*