WordPress.org

Ready to get started?Download WordPress

Forums

List all categories when on a page with categories (6 posts)

  1. dwk983
    Member
    Posted 4 years ago #

    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">
    	<?php
    	$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
    		if($post->post_parent)
    			$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    		else
    			$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">
    				<ul>
    					<li><a href="<?php echo $permalink; ?>"><?php echo $parent_title;?></a>&nbsp;></li>
    					<?php echo $children; ?>
    				</ul>
    			</div>
    	<?php } ?>
    </div>

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

    David Kirkpatrick

  2. MichaelH
    Member
    Posted 4 years ago #

    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

  3. dwk983
    Member
    Posted 4 years ago #

    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");
    	}

    Thanks,
    David Kirkpatrick

  4. MichaelH
    Member
    Posted 4 years ago #

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

  5. dwk983
    Member
    Posted 4 years ago #

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

    Any other ideas?

    David Kirkpatrick

  6. MichaelH
    Member
    Posted 4 years ago #

    There is no page name on a category archive.

    Related:
    Category Templates
    Template Hierarchy

Topic Closed

This topic has been closed to new replies.

About this Topic