Support » Fixing WordPress » List all categories when on a page with categories

  • Hi,

    I’ve got my menu bar to show the parent first, then all first tier subpages of the current page/subpage i’m on. However, when i’m on my blog page, I want the name of the blog page first, then all first tier categories to show instead.

    The following code is half working. Everything displays how I want it to when i’m on a normal page or subpage, but when i’m on the blog/category page I get a blank menu bar.

    <div id="horizontalmenu">
    	$cat = get_query_var('cat');
    	$category = get_category($cat);
    	//if you are on a page NOT a cat
    	if ($post->ID) {
    		$permalink = get_permalink($post->post_parent);
    		$parent_title = get_the_title($post->post_parent);
    		//if you are on the sub page display the parents sub pages
    		//if you are on a page display the sub pages
    			$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    			$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    	// if you are on a cat
    	if ($category->cat_ID) {
    		$permalink = get_permalink($category->category_parent);
    		$parent_title = get_the_title($category->category_parent);
    		$children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->category_parent."&echo=0");
    	if ($children) { ?>
    			<div id="hmenutext">
    					<li><a href="<?php echo $permalink; ?>"><?php echo $parent_title;?></a>&nbsp;></li>
    					<?php echo $children; ?>
    	<?php } ?>

    I’m new to php and wordpress, so i’m probably missing something obvious… thanks in advance for any help.

    David Kirkpatrick

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not sure if this is your problem, but might use is_page() and is_category() in place of if ($post->ID) { and if ($category->cat_ID) {.

    See Conditional Tags

    Thanks Michael. I’m just about there now. Everything works, but i’ve cheated slightly. In the following code ID 55 is my current post page (I have a static home page set, so the post page is not the home page). Can I replace the ’55’ with something? $page->ID doesn’t work. It pulls up the ID of the first post on the post page instead of the ID of the page name…

    // if you are on a cat
    if (is_home() or is_single() or is_category())  {
    	$permalink = get_permalink(55);
    	$parent_title = get_the_title(55);
    	$children = wp_list_categories("title_li=&echo=0");

    David Kirkpatrick

    Likely $posts[0]->ID would work if you were displaying blog posts.

    $posts[0]->ID gives me the first post on the page again, not the name of the page itself….

    Any other ideas?

    David Kirkpatrick

    There is no page name on a category archive.

    Category Templates
    Template Hierarchy

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘List all categories when on a page with categories’ is closed to new replies.