Tag Archives: specific

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 :)

Quiz: What is the keyboard shortcut to switch to a specific tab in your browser?

[wpsqt_quiz name="Switch to a particular tab in your browser"]

WordPress Tricks: How to exclude a certain category of posts from the loop

Use this line of code to control which categories of posts do and don’t show up in the loop for particular pages on your WordPress site:

<?php
if (is_page('recipes')) {
 $catID=4;
}
elseif (is_home()) {
 $catID=-4;
}
if ($catID) {
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=$catID&paged=$paged");
} ?>

Copy and paste that code into your index.php or page.php template file, just before the loop is called.

This line of code will display all posts in category 4 on the recipe page, and not on the main blogroll page.

Simply change the page IDs (or slugs, either one will work) and the category IDs to control which pages show which category of posts.

To exclude a category of posts from the loop on a certain page, simply put a negative sign ( – ) in front of the category ID you want to exclude from that page, and the page will show posts from all categories except for that one.

If you want to specify which page each category of posts show up on, you would use this:

<?php
$catID = 0;
if (is_page('Books')) {
 $catID=1;
}
elseif (is_page('Videos')) {
 $catID=4;
}
elseif (is_page('CDs')) {
 $catID=5;
}
if ($catID) {
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=$catID&paged=$paged");
} ?>

 

Let me know if you have any questions, need more help,  or run into any problems.

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.

 

 

WordPress: Display latest posts from a particular category in the sidebars of specific pages

After spending hours trying to figure out a way to display the latest posts from a certain category in WordPress on specific pages, I have finally found a solution!

First of all, it is important to note that the site I am working on is set up to display only posts from a particular category on specific pages.

The first thing I did was install the Exec-PHP plugin (which allows you to add php code to your sidebar widgets) and the Widget Logic plugin (which allows you to control which widgets show up on which pages’ sidebars).

Next, I added a “text” widget to my sidebar. I named the widget “Latest posts in Video.”

Then I pasted this code into the text area of the widget:

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

I changed the category ID (in this case: “7″) to the category ID that I wanted to display on the Video page.

Next, in the Widget Logic field of the widget, I used this line of code to tell the widget which page to appear on:

is_category(7)

That is where I had been going wrong. I had been using is_page(26) (which is the page ID where I have Video posts being displayed), but WordPress sees that page as a category page and not as a page page (because of the fact that I had it set up to show only a certain category of posts), so once I changed the Widget Logic code to is_category, rather than is_page, my solution worked perfectly.

As always, let me know if you have any questions or issues with this, I would be happy to help.