WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude Children Categories with next_post_link and previous_post_lin (7 posts)

  1. likemindsdesigns
    Member
    Posted 1 year ago #

    I am using this code:

    <?php previous_post_link('%link','%title',FALSE,'3 and 61 and 44'); ?>

    Category 44 is a child of category 61. If I only specify '3 and 61' then it does not exclude post links in children categories.

    Although I now have a temporary work-around by adding 'and 44' this will not work long term if my client adds additional children categories.

    I am a relative WP newbie, and brand new to PHP as well. Can anyone help me with some code to add to my functions.php page? I need something that will include children categories in the exclusion previous_post_link and next_post_link

  2. converting2wp
    Member
    Posted 1 year ago #

    A better PHP programmer than I can no doubt write this more compactly, but

    <?php
    $childcats  = get_categories(array('child_of' => 61));
    foreach ($childcats as $key => $cat) {
       $catids[$key] = $cat -> cat_ID;
    }
    $excludechildren = implode(' AND ',$catids);
    
    previous_post_link('%link','%title',FALSE,'3 AND 61 AND '.$excludechildren);
    ?>

    Refs: get_categories, implode

    You may also want to explore using the get_the_category function (if you don't want to hardcode the 61).

    Welcome to WP!

  3. likemindsdesigns
    Member
    Posted 1 year ago #

    Hi converting2wp

    Thank you for your help. I really appreciate it, but I am probably doing something wrong because that gave me this error message "Warning: implode() [function.implode]: Invalid arguments passed in /home/elbow/public_html/wp-content/themes/lmdelbowfoods/single.php on line 21"

    Maybe it's because i have that code inside of an if statement?

    This is my code in my single.php template page:

    <?php /* If post is in  recipes or products */ if(in_category(array( '61', '3' ))) :  {  /*don't display the nav bar at top*/  }
     else : { ?>
     <div id="blog-nav" style="border: none 0px;">
        		<?php
    			//function to exclude posts in children categories from next/previous post link
    			$childcats  = get_categories(array('child_of' => 61));
    			foreach ($childcats as $key => $cat) {
    			   $catids[$key] = $cat -> cat_ID;
    			}
    			$excludechildren = implode(' AND ',$catids); ?>
    <div id="prev"><?php previous_post_link('%link','%title',FALSE,'3 AND 61 AND '.$excludechildren); ?></div>
    <div id="next"><?php next_post_link('%link','%title',FALSE,'3 AND 61 AND '.$excludechildren); ?></div>
    		</div><!--/blog-nav-->
    <?php } endif; ?>
  4. converting2wp
    Member
    Posted 1 year ago #

    I only get that error if the category doesn't have any children that have posts. So my bad. I should have checked for that.

    What if you change my code to

    <?php
    $excludechildren="";
    $childcats  = get_categories(array('child_of' => 61));
    if (sizeof($childcats) > 0) {
          foreach ($childcats as $key => $cat) {
              $catids[$key] = $cat -> cat_ID;
          }
          $excludechildren = ' AND '.implode(' AND ',$catids);
    }
    previous_post_link('%link','%title',FALSE,'3 AND 61'.$excludechildren);
    ?>

    If your category 61 does have child categories that have posts, something else is going on.

    When you do want all the children of a given category (whether or not there are posts there), you may want to add "'hide_empty' => 0," to the array of parameters for the get_categories function.

  5. likemindsdesigns
    Member
    Posted 1 year ago #

    Yay! This is working for me.

    Can I trouble you for one more thing?

    How can I include child_of category 3 also?
    Is there a way to add this to array?

  6. converting2wp
    Member
    Posted 1 year ago #

    This isn't the most elegant code, but what if you replace the "$childcats=" line above with

    $childcats3  = get_categories(array('child_of' => 3));
    $childcats61 = get_categories(array('child_of' => 61));
    $childcats = array_merge($childcats3,$childcats61);

    See array_merge

  7. likemindsdesigns
    Member
    Posted 1 year ago #

    Yes, that worked perfectly. Thank You converting2wp for all your help. It is much appreciated!

    I am posting the final code in its entirety here if it will help anyone else:

    <?php
    			//function to exclude posts in children categories from next/previous post links
    			$excludechildren="";
    			$childcats3  = get_categories(array('child_of' => 3));
    			$childcats61 = get_categories(array('child_of' => 61));
    			$childcats = array_merge($childcats3,$childcats61);
    			if (sizeof($childcats) > 0) {
    				  foreach ($childcats as $key => $cat) {
    					  $catids[$key] = $cat -> cat_ID;
    				  }
    				  $excludechildren = ' and '.implode(' and ',$catids);
    			} ?>
    
    <?php previous_post_link('%link','%title',FALSE,'3 and 61'.$excludechildren); ?>
    <?php next_post_link('%link','%title',FALSE,'3 and 61'.$excludechildren); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.