• Resolved randycarl67

    (@randycarl67)


    I am unable to exclude a category from the previous_post_link and next_post_link in single.php.

    The category I want to exclude is id # 1 – named “View All”.

    The View All category is the default category for all posts.

    Every post is in 2 categories; a specific category like “Baby & Children” as well as the View All category.

    Here is the Previous and Next link code in single.php.

    <?php previous_post_link(‘%link’, ‘Previous’, TRUE, ‘1’); ?>
    <?php next_post_link(‘%link’, ‘Next’, TRUE, ‘1’); ?>

    I need to not show the “View All” category just the items specific category.

Viewing 10 replies - 16 through 25 (of 25 total)
  • Strange. That does not match what I just downloaded. Are you using ‘Fotofolio 1.0.8 by Pupung Budi Purnama’?

    Also, when you pasted in the fix, did you make sure there were no blank lines between the original end of the code and the start of the fix, and no blank lines after the fix?

    One reason I wanted to see the code including the fix was to see if anything got messed up in the copy/paste process.

    One other thing to check: do the calls to previous/next_post_link look like this?

    <?php previous_post_link('%link', wptp_getopt('nav_previous_value',0), wptp_getopt('nav_in_same_cat' , 0),'-1'); ?>

    Thread Starter randycarl67

    (@randycarl67)

    Nope. I’m using Fotofolio 1.0.6. And I had no spaces before or after the php code.

    I really appreciate all your time and effort on this but for some reason I guess I’m not gonna get this to work. I’ve the next/previous links on single.php.

    Maybe we’ll be able to have this in a future upgrade of WP.

    If you will zip up your theme folder and email it to me at m_a_mcdonald =at= bellsouth =dot= net, I will be happy to try to get it to work.

    For anyone else looking for a fix, first try the filter code posted above. If this does not work, and you don’t mind hacking the core files, here is what worked for randycarl67:

    In wp-includes/link-template.php, function get_adjacent_post, change this:

    if ( $in_same_cat ) {
       $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
       $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
    }

    to this:

    if ( $in_same_cat ) {
       $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
       if ( !empty($excluded_categories) ) {
          $temp_excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
          $new_cat_array = array_diff($cat_array, $temp_excluded_categories);
       }
       $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $new_cat_array) . ")";
    }

    For me is working another way without hacking the core: Duplicating, renaming and hacking the functions involved to functions.php from your template.

    Only need to insert “_2” to each function & the hack suggest by vtxyzzy in the “get_adjacent_post” function (our get_adjacent_post_2).

    function previous_post_link_2($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    	adjacent_post_link_2($format, $link, $in_same_cat, $excluded_categories, true);
    }
    function next_post_link_2($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    	adjacent_post_link_2($format, $link, $in_same_cat, $excluded_categories, false);
    }
    function adjacent_post_link_2($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
    	if ( $previous && is_attachment() )
    		$post = & get_post($GLOBALS['post']->post_parent);
    	else
    		$post = get_adjacent_post_2($in_same_cat, $excluded_categories, $previous);
    
    	if ( !$post )
    		return;
    
    	$title = $post->post_title;
    
    	if ( empty($post->post_title) )
    		$title = $previous ? __('Previous Post') : __('Next Post');
    
    	$title = apply_filters('the_title', $title, $post);
    	$date = mysql2date(get_option('date_format'), $post->post_date);
    	$rel = $previous ? 'prev' : 'next';
    
    	$string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">';
    	$link = str_replace('%title', $title, $link);
    	$link = str_replace('%date', $date, $link);
    	$link = $string . $link . '</a>';
    
    	$format = str_replace('%link', $link, $format);
    
    	$adjacent = $previous ? 'previous' : 'next';
    	echo apply_filters( "{$adjacent}_post_link", $format, $link );
    }
    function get_adjacent_post_2($in_same_cat = false, $excluded_categories = '', $previous = true) {
    	global $post, $wpdb;
    
    	if ( empty($post) || !is_single() || is_attachment() )
    		return null;
    
    	$current_post_date = $post->post_date;
    
    	$join = '';
    	$posts_in_ex_cats_sql = '';
    	if ( $in_same_cat || !empty($excluded_categories) ) {
    		$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
    
    //the hack
    if ( $in_same_cat ) {
       $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
       if ( !empty($excluded_categories) ) {
          $temp_excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
          $new_cat_array = array_diff($cat_array, $temp_excluded_categories);
       }
       $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $new_cat_array) . ")";
    }
    
    		$posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    		if ( !empty($excluded_categories) ) {
    			$excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
    			if ( !empty($cat_array) ) {
    				$excluded_categories = array_diff($excluded_categories, $cat_array);
    				$posts_in_ex_cats_sql = '';
    			}
    
    			if ( !empty($excluded_categories) ) {
    				$posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
    			}
    		}
    	}
    
    	$adjacent = $previous ? 'previous' : 'next';
    	$op = $previous ? '<' : '>';
    	$order = $previous ? 'DESC' : 'ASC';
    
    	$join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    	$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
    	$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    
    	$query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
    	$query_key = 'adjacent_post_' . md5($query);
    	$result = wp_cache_get($query_key, 'counts');
    	if ( false !== $result )
    		return $result;
    
    	$result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
    	if ( null === $result )
    		$result = '';
    
    	wp_cache_set($query_key, $result, 'counts');
    	return $result;
    }

    And the call in the template

    <div class="alignleft"><?php previous_post_link_2('%link', '<', TRUE,'3,10') ?></div>
    <div class="alignright"><?php next_post_link_2('%link', '>', TRUE,'3,10'); ?></div>
    Tzaddi

    (@zodzilla)

    Biochip, THANK YOU for that code. It worked beautifully to fix a bug I had in a site.

    Hello, I’m using 2.9.2 and the biochip function does indeed remove the categories from the previous and next leinks, but it fails to show the titles of the same, any suggestions?

    I have come up with a solution thats works for me. In order to exclude categories on the previous and next postlinks on a single post page, use:

    <?php previous_post_link(‘« %link’, ‘%title’, FALSE, ’13 and 16′) ?>

    <?php next_post_link(‘%link »’, ‘%title’, FALSE, ’13 and 16′) ?>

    This will exclude all posts from 13 and 16. If you wish to exclude more, simple continue that string 13 and 16 and 21 and 34 and so on.

    Hope this helps.

    Electric Studio thank you so much! I’ve been searching for hours to find this.

    Thanks Electric Studio, this little change makes my day! Also, big thanks to biochip for the code!

Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Can’t exclude a category from the previous_post_link and next_post_link’ is closed to new replies.