WordPress Tricks: How to add the name of parent category to the body class of a child category archive page

Here is the snippet of code I use to add a specific body class to the child category archive pages on a WordPress site. Paste this into the header.php file of your site’s theme:

<?php $this_category = get_category($cat);
if ($this_category->category_parent == 0) {
$this_category->category_parent = $cat;
} else {
$parent_category = get_category($this_category->category_parent);
$ParentCatId = $parent_category->cat_ID;
$ParentCatName = $parent_category->slug;
}
$childcategories = array();
$catx = $ParentCatId;
$cats = get_categories('parent='.$catx);
foreach($cats as $cat) {
$childcategories[] = $cat->term_id; } ;
if( is_category( $childcategories ) ) {
$class = 'child-category-of-'.$ParentCatName;
}
?> 

Next, make sure the body tag in the header.php file looks like this:

<body <?php body_class($class); ?>>

What this code will do is add the class “child-category-of-cooking-tips” to any archive page for subcategories of the category Cooking Tips.

Please let me know if you have any trouble implementing this into your site or if you are looking for customizations to this code.

A quick way to fix the broken pagination on your WordPress category or archive pages when using custom queries

Here is the useful code snippet that I use to fix the issue where using a custom query on a WordPress category or archive or even index page breaks the pagination.

Simply copy and paste this code into your theme’s functions file:

function my_post_queries( $query ) {
 // do not alter the query on wp-admin pages and only alter it if it's the main query
 if (!is_admin() && $query->is_main_query()){
// alter the query for the home page
 if(is_home()){
 $query->set('posts_per_page', 3);
 $query->set('cat', '9');
 }
}
add_action( 'pre_get_posts', 'my_post_queries' );

And then edit as necessary!

To target a specific category, you would use this code:

 // alter the query for the Apples category page 
    if(is_category('Apples')){
      $query->set('posts_per_page', 6);
    }

I have found this to be one of the easiest ways to fix the pagination issues I have run into when using custom WordPress queries. Let me know if you have a different or better way of doing so :)

Source

 

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.'&<span style="color: #ff0000;">cat=4</span>' ); } ?>

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

  • elements when you use wp_nav_menu to call in a menu:

    <?php $custommenu = wp_nav_menu ( array (
    		'container_class' => 'my-menu',
    		'theme_location' => 'primary',
                    '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 :)

    Joomla Tips: How to assign body IDs to each page

    Here is the code I use to add an individual ID to each page in a Joomla website. In index.php of your site’s template folder, add this code in place of the < body> tag:

    <?php
    $menu = &JSite::getMenu();
    $active = $menu->getActive();
    ?>
    <body id="<?php print $active->alias; ?>">

    Now, each page will have it’s own ID. For example, www.yourwebsite.com/about-us will have the ID “about-us.”

     

    PHP Tips: How to send an email through a contact form to multiple recipients

    Here is the simple line of code you can use to send a PHP email contact form to multiple email addresses:

      $to = $this->fields['EmailAddress'].',';
    	  $to .='info@yourwebsite.com';

    This comes in handy when you have to send the email to one specific email address in addition to an email address which is being pulled in via a variable – for example, if you want to send a copy of the email to users themselves, as in the example above.

    PHP Tips: How to convert underscores to spaces in emailed contact forms

    I was recently building a PHP contact form and ran into the issue where the field names I was using in my HTML form which we were more than one word long (i.e.: “First Name”) were being emailed with underscores in place of the spaces (i.e.: “First_Name”). The PHP file I was processing my form with was replacing the spaces with underscores. Which was necessary for processing the form, BUT – I needed to convert those underscores back to spaces when the contact form data was emailed for usability purposes.

    Here is the simple line of code that I added to my PHP file:

    $msg = str_replace("_"," ", $msg);

    Where $msg was part of my mail function:

     mail($to, $subject, $msg, $headers)

    And had already been defined:

      $msg = "User message: \n\n";
              foreach($this->fields as $key => $field)
                    $msg .= "$key:  $field \n";

    I added the first line of code (which used the str_replace function) right after the code which defined $msg.

    You will want to replace the variable $msg in the code above with the name of the variable you are using for the body of the email in your mail function.

    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.

    Concrete5: How to display a page’s unique ID

    Here is the line of code I use to display a page’s unique ID.

    <?php echo $c->getCollectionID() ?>

    This is really useful for styling individual pages.