Support » Plugin: WP Category Permalink » Print chosen category label in single.php

  • Hello,

    I want to print the chosen category in single.php. What code would I use to do that? Is there a way to use get_the_category() to return the category your plugin sets for a post?

    Great plugin and thanks,

    Dean

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter frankin-grep

    (@boulderpoet)

    Hi,

    This filter should work, but does not. It should force get_the_category() in single.php to return the plugin chosen category as the first in the list. Can you tell me what I’m doing wrong?

    function my_get_the_categories( $categories ){
    	
    	global $post;
    	$main_category = array();
    	$main_category_id = get_post_meta($post->ID, '_category_permalink', true);
    	
    	if( ! empty( $categories ) ) {
    		foreach( $categories as $key => $category ) {
    			if( $category->term_id == $main_category_id ) {
    				$main_category[] = $category;
    				unset($categories[$key]);
    			}
    		}
    	}
    
    	return array_merge($main_category, $categories);
    
    }
    add_filter('get_the_categories', 'my_get_the_categories');

    Thanks,

    Dean

    Thread Starter frankin-grep

    (@boulderpoet)

    So here’s my working solution to print the primary category in single.php

    
    $primary_cat_meta = get_post_meta($post->ID,"_category_permalink",true); // returns array of primary cat ID	
    extract($primary_cat_meta); // extracts integer $category which is the primary cat ID
    get_cat_name($category); // returns primary cat name to print on page
    
    

    Hope this is helpful!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Print chosen category label in single.php’ is closed to new replies.