Forums

Category Navigation on single.php (17 posts)

  1. deftru
    Member
    Posted 1 year ago #

    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 ( http://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>

  2. Digital Raindrops
    Member
    Posted 1 year ago #

    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

  3. deftru
    Member
    Posted 1 year ago #

    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

  4. royalprince
    Member
    Posted 1 year ago #

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

    You can modify it to suit your purpose

  5. deftru
    Member
    Posted 1 year ago #

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

  6. Digital Raindrops
    Member
    Posted 1 year ago #

    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

  7. deftru
    Member
    Posted 1 year ago #

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

    http://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

  8. MAS
    Member
    Posted 1 year ago #

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

    would be

    $postcat= $category->term_id;
    $catname =$category->name;
  9. MAS
    Member
    Posted 1 year ago #

    <?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

  10. deftru
    Member
    Posted 1 year ago #

    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:
    http://www.goodandbad.net/sport-akustik/limited/referenz/

    THX

  11. deftru
    Member
    Posted 1 year ago #

    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

  12. MAS
    Member
    Posted 1 year ago #

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

  13. deftru
    Member
    Posted 1 year ago #

    PERFECT!!!! thank you so much!

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

  14. MAS
    Member
    Posted 1 year ago #

    you are welcome.:)

    IF solve, plz mark your 2 threads as RESOLVED.

  15. deftru
    Member
    Posted 1 year ago #

    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

  16. Digital Raindrops
    Member
    Posted 1 year ago #

    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

  17. deftru
    Member
    Posted 1 year ago #

    a little problem..
    the result is perfect.. just one tiny little thing.

    at the end it writes one subcategory with <h2>
    but i just want to display the parent category with <h2> on top (Ltd.)

    have a look:
    http://www.goodandbad.net/sport-akustik/limited/perfekte-akustik-im-fur-forschung-und-lehre/

    <?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;
    }
    ?>

    Presse should not be displayed.
    would be glad if this could be fixed.

Topic Closed

This topic has been closed to new replies.

About this Topic