Tag Archives: wordpress

WordPress Tricks: Use a WordPress query to exclude a category from your home page without breaking pagination

Here is the code I use in a WordPress site’s index.php file to exclude a specific category from the home page withOUT breaking the pagination:

<?php if ( is_home() ) { query_posts( $query_string.'&cat=-21' ); } ?>

Of course, to include a specific category, you would use this:

<?php if ( is_home() ) { query_posts( $query_string.'&cat=4' ); } ?>

WordPress Tips: How to remove the extra space between LI elements from wp_nav_menu

Here is the snippet of code I use to remove the extra space that WordPress adds to the end of <li> elements when you use wp_nav_menu to call in a menu:

<?php $custommenu = wp_nav_menu ( array (
               'menu'            => 'my-custom-menu',
		'container'	=> '',
                'container_id'    => '',
                'menu_class'      => '',
                'menu_id'         => '',
                'echo'            => 0
 ) );
$custommenu = str_replace("\n", "", $custommenu);
$custommenu = str_replace("\r", "", $custommenu);
echo $custommenu;
?>

 

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: Add a div or other element to one specific page or page type

 

Here is the code I use to add a div or image or other element to just one page or category page of my WordPress sites:

This code will add a div with the id “featured_content” only on the home page:

<?php if ( is_home() ) { ?>
<div id="featured_content">
&nbsp;
</div>
<?php } ?>

 

This code would add a div with the id “featured_recipe” to all posts in the category “recipes.”:

<?php if ( is_category('recipes') ) { ?>
<div id="featured_recipe">
&nbsp;
</div>
<?php } ?>

 

This code would exclude “myimage.jpg” only on the Contact page:

<?php if ( !is_page('contact') ) { ?>
<img src="myimage.jpg">
<?php } ?>

 

As you can see, there are many uses and ways implementations of this code. Let me know if you have any questions or need further assistance.

WordPress Tricks: How to fix the issue of YouTube videos appearing on top of other content

Here is the code I use to make sure the embedded YouTube videos on a website don’t appear on top of other content (for example: drop down menus):

<object width='400' height='350'> 
    <param name='movie' value='http://www.youtube.com/YOURVIDEOLINK'> 
    <param name='type' value='application/x-shockwave-flash'> 
    <param name='allowfullscreen' value='true'> 
    <param name='allowscriptaccess' value='always'> 
    <param name="wmode" value="opaque" />
    <embed width='400' height='350'
            src='http://www.youtube.com/YOURVDIDEOLINK'
            type='application/x-shockwave-flash'
            allowfullscreen='true'
            allowscriptaccess='always'
            wmode="opaque"
    ></embed> 
    </object>

The areas of code highlighted in red above are what solve the overlapping/z-index problem, so if you already have your video embedded on your site, simply add those sections in the right place in your code and that should solve the issue.

NOTE: I found this solution at http://australiansearchengine.wordpress.com/2010/06/19/youtube-video-css-z-index/

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.