WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_category_link in single (5 posts)

  1. ceslava
    Member
    Posted 6 years ago #

    When updated to WP 2.5 the function get_category_link($cat) when use in single it doesn´t work anymore. It returns the category base which means is like this post it doesn´t belong to any category, returns ID 0.

    Thank you in advance

  2. ceslava
    Member
    Posted 6 years ago #

    I resolved it by creating a new function based on the_category which works perfectly in a single.

    This is the only way I could extract the category link in a single

  3. zeroo
    Member
    Posted 6 years ago #

    Hi ceslava,
    I have the same problem.
    Please can you post that function here ?
    Thanks

  4. ceslava
    Member
    Posted 6 years ago #

    Sure, zeroo,

    function get_categoria_post($separator = '', $parents='') {
    	global $wp_rewrite;
    	$categories = get_the_category();
    	if (empty($categories))
    		return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
    
    	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
    
    	$thelist = '';
    	if ( '' == $separator ) {
    		$thelist .= '';
    		foreach ( $categories as $category ) {
    			$thelist .= "";
    			switch ( strtolower($parents) ) {
    				case 'multiple':
    					if ($category->parent)
    						$thelist .= get_category_parents($category->parent, TRUE);
    					$thelist .=  get_category_link($category->term_id);
    					break;
    				case 'single':
    					$thelist .= get_category_link($category->term_id);
    					if ($category->parent)
    						$thelist .= get_category_parents($category->parent, FALSE);
    					$thelist .= $category->name.'</a></li>';
    					break;
    				case '':
    				default:
    					$thelist .= get_category_link($category->term_id);
    			}
    		}
    		$thelist .= '';
    	} else {
    		$i = 0;
    		foreach ( $categories as $category ) {
    			if ( 0 < $i )
    				$thelist .= $separator . ' ';
    			switch ( strtolower($parents) ) {
    				case 'multiple':
    					if ( $category->parent )
    						$thelist .= get_category_parents($category->parent, TRUE);
    					$thelist .= get_category_link($category->term_id);
    					break;
    				case 'single':
    					$thelist .= get_category_link($category->term_id);
    					if ( $category->parent )
    						$thelist .= get_category_parents($category->parent, FALSE);
    					$thelist .= "$category->cat_name</a>";
    					break;
    				case '':
    				default:
    					$thelist .=get_category_link($category->term_id);
    			}
    			++$i;
    		}
    	}
    	return apply_filters('the_category', $thelist, $separator, $parents);
    }

    Check it out in justtellmewhy.com

  5. razorguy
    Member
    Posted 5 years ago #

    <a href="<?php echo get_option('home'); ?>">Home</a>&nbsp;&rsaquo;&nbsp;&nbsp;<?php the_category('&nbsp;&rsaquo;&nbsp;&nbsp;', 'multiple' ); ?>&nbsp;&rsaquo;&nbsp;&nbsp;<?php the_title(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic