display all subcategories within posts on single.php
-
Hi!
I need a code to display all my subcategories within posts for single.php:
like this:
subcategory 1(if existing)
– post 1
– post 2
– post n
subcategory 2(if existing)
– post 1
– post 2
– post n
subcategory n(if existing)
– post 1
– post 2
– post nthanks for respone
-
in general:
get the category id of the categories of the post
http://codex.wordpress.org/Function_Reference/wp_get_post_categoriesforeach of the above – get the sub categories
http://codex.wordpress.org/Function_Reference/get_categories
‘child_of’ parameter to include sub-sub cats
‘parent’ parameter to get only direct sub categoriesforeach of these sub categories – get the posts
http://codex.wordpress.org/Template_Tags/get_posts
‘numberposts=-1’ to show all posts
‘cat=’ to get the posts of the subcategoryhi alchymyth!
thanks 4 your response. i do have a code working on the category.php. but -and i am more webdesigner than programmer 😉 – i am not sure why it doesn’t work on the single.php.
maybe you could have a look and help me out: THX
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 } ?><?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; } ?>write this in single.php file.
see this also this code for category.php or archive.php
http://wordpress.org/support/topic/custom-category-page-display-subcategories-with-posts?replies=7also see this http://wordpress.org/support/topic/category-navigation-`on-singlephp?replies=9
$postcat= $category->cat_ID;
$catname =$category->cat_name;`would be
$postcat= $category->term_id; $catname =$category->name;problem solved!
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
a little problem..the result is perfect.. just one tiny little thing:
at the end (have a look:
http://www.goodandbad.net/sport-akustik/limited/perfekte-akustik-im-fur-forschung-und-lehre/)
it writes one subcategory (presse)with with the style of the category (<h2>)but i just want to display the parent category with <h2> on top (Ltd.)
<?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.replace this
echo "<h1>". $cat->name . "</h1>";withecho "<h2>". $cat->name . "</h2>";no, that is not the problem.:)
the problem is that the function :
echo "<h1>". $cat->name . "</h1>";
writes 2 categories. one at the beginning – which is good and one at the end (which is a subcategory in category-style) .i think the foreach function is placed wrong. hope u know what i mean.
thx anywayOne will show parent category name and another will be subcategory name.
Now it is display this way.chnage this line
echo "<h1>". $cat->name . "</h1>";with thisif($cat->parents) echo "<h1>". $cat->name . "</h1>";nope. writes nothing..
everything else is fine. i just need the parent category. and best outside any loop.
echo(get_category_parents($cat, TRUE, ' » '));writes for example
category >>
subcat
post
subcat
post
category >> subcategoryI am so busy. If you want to contact with me, sent mail chinmoy29 at gmail dot com
thanks ! but the code do not work for me !
you can help me to check my site : http://muasamvui.com
The topic ‘display all subcategories within posts on single.php’ is closed to new replies.