Viewing 3 replies - 1 through 3 (of 3 total)
  • Josie

    (@jlcastogmailcom)

    I wrote this function to solve this problem (excuse me if I’m not following php best practices, I’m new to php…old pro at JSPs though)

    function get_current_location(){
    	$locations_cat_id = 2;
    	$ignore_ids = array();
    	$ignore_ids[] = $locations_cat_id;
    	$cats = get_categories( array( 'child_of'=>$locations_cat_id, 'parent'=>$locations_cat_id, 'hide_empty'=>0 ) );
    	foreach($cats as $c){
    		$ignore_ids[] = $c->cat_ID;
    	}
    
    	foreach((get_the_category()) as $category) {
    		if ( !in_array($category->cat_ID, $ignore_ids) && cat_is_ancestor_of( $locations_cat_id, $category->cat_ID )) {
    			return $category->cat_name;
    		}
    	}
    	return "";
    }
    Josie

    (@jlcastogmailcom)

    So, really I should have narrowed the above down to what was really getting the current page’s categories…I was up late, forgive me.

    foreach ((get_the_category()) as $category) {
    //Do something with your category here
    //You could also pass in a page ID to get_the_category to get the categories for another page
    }
    Plugin Author Amit Verma

    (@amit_ish)

    Josie is correct.

    global $post;
    $cats=array();
    foreach(get_the_category() as $category) {
    	echo '<a href="'.get_category_link($category->term_id ).'">'.$category->cat_name.'</a>';
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Map Categories to Pages] display page category’ is closed to new replies.