Tag Archives: get

WordPress Tricks: How to Show Most Recent Posts from Specific Categories with Thumbnails

Here is the code I use to show a certain number of the most recent posts including their thumbnails from specific categories in a WordPress website.

To show the 3 most recent posts from Category 1, you would add this code to the appropriate template file of your site:

<?php
//for category 1, show 3 posts
$cat_args=array(
  'include' => '1',
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' =>3,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p><h4>Latest <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.' Posts</a>: </h4></p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

You will want to edit the category ID(s) in this code of course so that you call the posts from the correct categories of your site, and you may also want to edit the number of posts shown from the categories as well.

To show the 3 most recent posts from Categories 1, 2, AND 3, you would use this code:

<?php
//for categories 1, 2, and 3, show 3 posts
$cat_args=array(
  'include' => '1,2,3',
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' =>3,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p><h4>Latest <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.' Posts</a>: </h4></p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

If you want to include the excerpt of each post, you would use this code:

 

<?php
//for category 1, show 3 posts
$cat_args=array(
  'include' => '1',
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' =>3,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p><h4>Latest <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.' Posts</a>: </h4></p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p class="recent-posts"><span class="thumb"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?></span>
<span class="recent-post"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></span>
<br/><span class="excerpt"><?php the_excerpt(); ?></span></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

Let me know if you have any questions or need any further modifications to this code :)

WordPress Tricks: Get the titles of recent blog posts from a certain category using wp_query

Here is a simple block of code that you can use to get the most recent post titles and permalinks to a certain number of the most recent posts in a certain category:

 

<ul>
<?php
    global $cat;
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5&cat=389');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?></ul>

Simply change the number of posts and the category id in the code above to get the number of posts and the category of posts that you want.

 

You can also check out my post on how to display a list of the titles of your most recent blog posts from several categories if you are looking for a way to get the most recent posts from several different categories.

Get and display the most recent comments from your WordPress blog

Here is the code that I use to get and display the most recent comments from my WordPress blog:

<?php
$recent_comments = get_comments( array(
  'number'    => 5,
  'status'    => 'approve',
  'type'    => 'comment'
) );

foreach ($recent_comments as $comment)
{
?>
<li>
<a href="<?php echo get_permalink($comment->comment_post_ID);?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>">
<?php echo get_avatar( $comment->comment_author_email, '55' ); ?>
</a>
<p><b>
<?php echo($comment->comment_author); ?>
 said:</b></p>
<p>
<?php echo($comment->comment_content); ?>
</p>

<span style="font-size:12px;font-weight:bold;font-style:italic;">
<a href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php echo $comment->comment_ID;?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>">
<?php echo get_the_title($comment->comment_post_ID); ?>
</a>
</span>
</li>
<?php
}
?>
</div>

Let me know if you have any questions or need help customizing the code.

WordPress Tricks: How to call and display one specific post

Here is the line of code I use to display the content of one particular post somewhere on my WordPress website:

<?php
$post_id = 26;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;
?>

You will obviously need to replace the post ID (26) in the code above with the post ID of the post you want to call.