• Hi!

    I have a tricky question. I use a category-based navigation with subcategories and posts. I have the category navigation on top and a local navigation (on the left) shown below. what i need is that the working code for the category-page also works on the single.php or i use a different one with the same result:

    THATS WHAT I WANT ( https://www.goodandbad.net/sport-akustik/category/know-how/ ):
    title category parent category
    – posts in category
    subcategory 1(if existion)
    – posts in subcategory
    subcategory 2(if existion)
    – posts in subcategory

    HERE IS THE CODE:

    <backticks>

    <h2><?php echo $catname; ?></h2>

    <?php $categories = get_categories(“child_of=5”); foreach ($categories as $cat) { ?>

    <?php query_posts(“cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name”); ?>
    <h3><?php single_cat_title(); ?></h3>
    <?php while (have_posts()) : the_post(); ?>

    <div class=”what”>
    <h3>” rel=”bookmark” title=”Permanent Link to
    <?php the_title_attribute(); ?>”><?php the_title(); ?>
    </h3>
    </div>
    <?php endwhile; ?>
    <?php } ?>

    </backticks>

Viewing 15 replies - 1 through 15 (of 16 total)
  • Un-tested but something like this, as the page is already filtered on a single category?

    <?php $currcategory = get_the_category($post->ID); ?>
    <?php $catid = $currcategory->cat_ID; ?>
    <?php $catchildren= 'child_of="'.$catid.'"'; ?>
    <?php $categories = get_categories($catchildren); foreach ($categories as $cat) { ?>
         <!-- Do Loop Stuff -->
    <?php } ?>
    <?php endif; ?>

    HTH

    David

    Thread Starter deftru

    (@deftru)

    Thanks David for your code.
    Unfortunately the result is that nothing is displayed.
    the complete code in the sidebar is:

    foreach((get_the_category()) as $category)
    	{
        $postcat= $category->cat_ID;
        $catname =$category->cat_name;
        }
    <h2><?php echo $catname; ?></h2>
    	<?php $categories = get_categories("child_of=5"); foreach ($categories as $cat) { ?>
    	<?php query_posts("cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name"); ?>
    		<h3><?php single_cat_title(); ?></h3>
    		<?php while (have_posts()) : the_post(); ?>
    
    		<div class="what">
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
    			<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    		</div>
    	<?php endwhile; ?>
    <?php } ?>

    to make myself clear:
    a code on the single.php which echos the subcategory and its posts would be great

    Visit https://www.z-oc.com/blog/zoc-subcats/

    You can modify it to suit your purpose

    Thread Starter deftru

    (@deftru)

    @royalprince: i need the subcategories and the posts within .. but thanks

    I was off base here, I thought it was the category in the top menu and the sub categories in the sidebar, like a sidebar sub menu.

    But you want a sub category header and then the post titles in the sidebar?

    I am not sure now of what you are after, is this just the page content or page content and sidebar you want?

    EDIT: I get it now, I have some code that does this in the content area, I am ar work now will have a look later.

    HTH

    David

    Thread Starter deftru

    (@deftru)

    Hi David. Thanks for your response. To make it clear look at my textpage :

    https://www.goodandbad.net/sport-akustik/category/know-how/

    On the left site you see the navigation structure

    Title current category
    -title subcategory 1
    posts within
    -title subcategory 2
    posts within

    and this is what i want to see on my single.php

    thanks

    $postcat= $category->cat_ID;
    $catname =$category->cat_name;

    would be

    $postcat= $category->term_id;
    $catname =$category->name;

    <?php
    $post_categories = wp_get_post_categories( $post->ID );
    foreach($post_categories as $c){
      $cat = get_category( $c );
      echo "<h1>". $cat->name . "</h1>";
      $childCats = get_categories( array('child_of' => $c) );
      if(is_array($childCats)):
        foreach($childCats as $child){ ?>
            <h2><?php echo $child->name; ?></h2>
    <?php
            query('cat='.$child->term_id);
            while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
             //post code stuff here;
            endwhile;
            wp_reset_query();
        }
      endif;
    }
    ?>

    write this on your single.php file

    Thread Starter deftru

    (@deftru)

    thanks chinmoy!

    the result looks good. unfortunately the posts on the single.php are not going anywhere. it always shows the last post but i can’t go to the e.g. first post of the subcategory..

    look at:
    https://www.goodandbad.net/sport-akustik/limited/referenz/

    THX

    Thread Starter deftru

    (@deftru)

    thanks chinmoy again!

    the fatal error msg:
    Fatal error: Call to undefined function: query() in /homepages/4/d188254744/htdocs/sport-akustik/wp-content/themes/Q/left-1.php on line 22

    thx

    sorry!! change query('cat='.$child->term_id); to
    query_posts('cat='.$child->term_id);

    Thread Starter deftru

    (@deftru)

    PERFECT!!!! thank you so much!

    I would be glad if I could also understand it;-)

    you are welcome.:)

    IF solve, plz mark your 2 threads as RESOLVED.

    Thread Starter deftru

    (@deftru)

    THIS IS THE FUNCTIONING CODE FROM chinmoy29:

    <?php
    $post_categories = wp_get_post_categories( $post->ID );
    foreach($post_categories as $c){
      $cat = get_category( $c );
      echo "<h1>". $cat->name . "</h1>";
      $childCats = get_categories( array('child_of' => $c) );
      if(is_array($childCats)):
        foreach($childCats as $child){ ?>
            <h2><?php echo $child->name; ?></h2>
    <?php
            query_posts('cat='.$child->term_id);
            while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
             //post code stuff here;
            endwhile;
            wp_reset_query();
        }
      endif;
    }
    ?>

    THX

    Glad you have it sorted,
    I just looked and when you click on the link you loose the side menu structure top down, as the post id and post categories change.

    Don’t you really need to check if there is any parents using the get category parents, return nice name and get categories by slug, and then roll down from the top level?

    We just had a three hour local power cut so my evening is shot!

    David

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Category Navigation on single.php’ is closed to new replies.