10 WordPress Displaying Codes you must know

by Saad Irfan

No denial, customizing a WordPress theme is no more hectic work; thanks to those bloggers those have given scripts on their blogs free of cost. But if you are willing to design a theme on your own or wants little extra customization in your blog, here are few WP hacks which you must know in order to proceed. These scripts are ready and good to go; all you need to do is to place these scripts in your pages where you want them to execute.

1) Display Recent Posts:

<?php wp_get_archives(‘type=postbypost&limit=10’); ?>

The script given above displays last 10 posts, limit can be change.

2) Display Recent Posts from Selected Category only:

<?php  $recentpostsbycat = get_posts(‘numberposts=5&category=1’);

foreach ($recentpostsbycat as $post) :

setup_postdata($post);  ?>

<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>

<?php endforeach; ?>

“numberposts” set the limit of posts

“catagory”  set the category ID from which you want to call posts.

3) Display Recent Comments:

We have already discussed it with detail in our previous post.

http://techcline.com/personal-research/334/how-to-call-recent-comments-from-database-in-wordpress.html

or here is another way,

<?php  global $wpdb;  $sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID,  comment_post_ID, comment_author, comment_date_gmt, comment_approved,  comment_type,comment_author_url,  SUBSTRING(comment_content,1,30) AS com_excerpt  FROM $wpdb->comments  LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =  $wpdb->posts.ID)  WHERE comment_approved = ‘1’ AND comment_type = ” AND  post_password = ”  ORDER BY comment_date_gmt DESC  LIMIT 10″;  $comments = $wpdb->get_results($sql);  $output = $pre_HTML;  $output .= “n<ul>”;  foreach ($comments as $comment) {  $output .= “n<li>”.strip_tags($comment->comment_author)  .” says:” . “<a href=”” . get_permalink($comment->ID) .  “#comment-” . $comment->comment_ID . “” title=”on ” .  $comment->post_title . “”>” . strip_tags($comment->com_excerpt)  .”</a></li>”;  }  $output .= “n</ul>”;  $output .= $post_HTML;  echo $output;?>

4) Display Recent Posts

<?php wp_get_archives(‘type=postbypost&limit=5’); ?>

5) Display Pages Menu:

<h1>Site Pages</h1>

<ul> <?php wp_list_pages(‘title_li=’); ?> </ul>

6) Display Parent Categories only:

If you interested into displaying Parent Categories only, do check our Previous Post

http://techcline.com/personal-research/355/list-parent-categories-only-in-list-and-in-drop-down-menu-wordpress.html

7) Display All Categories:

<h1>Site Categories</h1>

<ul><?php wp_list_cats(‘sort_column=name’); ?> </ul>

8 ) Display Complete Archives:

<h1>Site Archives</h1>

<ul><?php wp_get_archives(‘type=monthly’); ?> </ul>

9) Display WordPress Tags Cloud

<?php wp_tag_cloud(‘smallest=10&largest=20&’); ?>

And like alwys you can do modification here as well. You can change Values for variable “smallest” and “largest”.

10) Display Admin Section Links

<ul>  <?php wp_register(); ?>  <li><?php wp_loginout(); ?></li>  </ul>