6

I have hierarchy like this: Category1 -Subcategory1 -Subcategory2 --Post1 Category2 -Subcategory1

I need to show subcategories name and description when i go to category//// Now i use this code:

    <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo '<div id="catlist"><ul>';
            $childcategories = get_categories(array(
                'orderyby' => 'name',
                'hide_empty' => false,
                'child_of' => $this_category->cat_ID
            ));

        foreach($childcategories as $category) {
            echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
            echo '<p>'.$category->description.'</p>';
        }
        echo '</ul></div>';
    }
}
?>

Now i have subcategories list when click on category and blank page if i click on subcategory link but i need to show posts when i click on subcategory list.

  • Do you want to show the posts of the sub-category or just a general list of posts? – ngearing Jun 6 '15 at 10:50
  • 1
    what template file are you editing? if it is category.php, does it also contain the normal loop? – Michael Jun 6 '15 at 12:13
  • I want to show only subcategories when i click on category links and i want to show posts when i click on subcategory link. I edit category.php – Dima Skvarskyi Jun 6 '15 at 12:40
1

Your code checks for child categories and shows them if they exist, but you aren't then going on to process the loop that would ordinarily display the posts in the current category.

You want to extend your code to be of the form:

if ( is_category() ) {

    $this_category = get_category($cat);

    if ( get_category_children( $this_category->cat_ID ) != "" ) {

        // display the list of child categories as you currently do

    } else {

        /* run the standard loop to show the posts using
           whatever loop code your other templates use
        */

    }
}
0
<div id="content" role="main">
       <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        echo '<div id="catlist"><ul>';
            $childcategories = get_categories(array(
                'orderyby' => 'name',
                'hide_empty' => false,
                'child_of' => $this_category->cat_ID
            ));
        foreach($childcategories as $category) {
            echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
            echo '<p>'.$category->description.'</p>';
        } echo '</ul></div>';
    }
    else{       
        get_template_part('loop-header');   
        if (have_posts()) :
            get_template_part('loop-actions');
            get_template_part('loop-content');
            get_template_part('loop-nav');
        else :
            get_template_part('loop-error');
        endif;  }}?>
<?php 
    ?>
    </div>
  • Can you please explain what this code does (in an edit)? Code only without context is not really what helps most people :) Thanks. – kaiser Sep 5 '17 at 19:34
0
if (is_category()){
$category = get_queried_object();
$subcategories = get_category_children( $category->term_id );
// var_dump($subcategories);
if ( $subcategories != "" ) {
    echo '<div id="catlist"><ul>';
    $childcategories = get_categories(array(
        'orderyby' => 'name',
        'hide_empty' => false,
        'child_of' => $category->term_id,
    ));
    foreach( $childcategories as $single ) {
       echo '<a href="' . get_category_link( $single->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $single->name ) . '" ' . '>' . $single->name.'</a>';
       echo '<p>'.$single->description.'</p>';
    }
    echo '</ul></div>';
} else {
// your loop.
}
-1
    <?php if ( have_posts() ) : ?><!-- .page-header -->
            <div class="clearfix enclose">
            <?php
            // Start the Loop.
            while ( have_posts() ) : the_post(); ?>
                <div class="sub-category-block">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('medium_thumbnail'); ?>
                    <p class="title"><?php the_title(); ?></p>
                    </a>
                </div>
            <?php endwhile; ?>                        
            </div>
    <?php else : ?>
        <div class="page-header">
            <?php $args = array(
                'posts_per_page'   => 5,
                'offset'           => 0,
                'category'         => '',
                'category_name'    => '',
                'orderby'          => 'date',
                'order'            => 'DESC',
                'include'          => '',
                'exclude'          => '',
                'meta_key'         => '',
                'meta_value'       => '',
                'post_type'        => 'post',
                'post_mime_type'   => '',
                'post_parent'      => '',
                'author'       => '',
                'post_status'      => 'publish',
                'suppress_filters' => true 
                );?>
            <?php query_posts( $args ); ?>
            <?php while ( have_posts() ) : the_post(); ?>
            <?php endwhile; wp_reset_query(); ?>
        </div><!-- .page-header -->
    <?php endif; ?>

create category template file for e.g category-abc.php and insert the above code where you will get all subcategory of the repesctive parent category and when you click the sub-category the post of the sub category will be displayed

  • 1
    When you use query_posts(), you will actively override the main query for this request. This is something you should not do. Better use new \WP_Query( array( /* args */ ) ) instead. – kaiser Jul 21 '16 at 12:18
  • it's ok @kaiser i agree for that but the solution which i have given is working or not. did you tried.. as for me the above query worked well – Pullata Praveen Jul 21 '16 at 12:22
  • 1
    You might want to read this entry about the problems you will be facing by overriding the main query. And if you want to alter arguments for the main query, there are filters that make it much safer to work on changing the main query. – kaiser Jul 21 '16 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.