Very short snippet: Display a list of posts by category or tags

Two useful features, customizable at will, to be used to show the last n posts of a category or one or more tags. Functions, as you will see, are very similar and are both based on a loop generated by query_post() . The first, show_title_cat() , shows the last post (in this version only the title) of a specific category:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/ **
* Show the last posts of a certain category
*
* @ Return
* @ Param object $ cats
* @ Param object $ show [optional]
* /
show_title_cat function ($ cats, $ show = 5) {
global $ post;

$ Posts = query_posts ('cat ='. $ Cats. 'Showposts & ='. $ Show);
echo "<ul>";
foreach ($ posts as $ post): setup_postdata ($ post);?>
; ?> " rel="bookmark"> <?php the_title ( ) ; ?> </a></li> <li> <a href = "<? php the_permalink ();?>" rel = "bookmark"> <? php the_title ();?> </ a> </ li>
; <? Php endforeach;
; echo "</ ul>";
}

For example:

1
'748' ) ; ?> <? Php show_title_cat ('748 ');?>

The second function, perhaps more interestingly, shows the last post filtering by tag:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/ **
* Show the last posts with a particular tag
*
* @ Return
* @ Param object $ tag
* @ Param object $ show [optional]
* /
show_title_tag function ($ tag, $ show = 5) {
global $ post;

$ Posts = query_posts ('tag ='. $ Tag. 'Showposts & ='. $ Show);
echo "<ul>";
foreach ($ posts as $ post): setup_postdata ($ post);?>
; ?> " rel="bookmark"> <?php the_title ( ) ; ?> </a></li> <li> <a href = "<? php the_permalink ();?>" rel = "bookmark"> <? php the_title ();?> </ a> </ li>
; <? Php endforeach;
; echo "</ ul>";
}

For example:

1
'photoshop,tutorials' ) ; ?> <? Php show_title_tag ('photoshop tutorials');?>

2 comments: "Very short snippet: Display a list of posts by category or tags"

  1. July 2, 2009 Undolog.com »Wordpress: remove duplicate posts in multiple loops :

    [...] WordPress allows you to get a list of post setting several search parameters (see Very short snippet: Display a list of posts by category or tags). Precisely because of its versatility in the search of the posts may be that using the [...]

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL