• Resolved ramponi

    (@ramponi)


    The code below shows the subcategories linked to the selected category. And when clicking on the subcategory link it will show the subcategories of that selected subcategory.

    And so on. However, he is not showing the posts when he is at the last level of the subcategory.

    How do I get this code to bring posts related to the selected subcategory?

    For example:

    The category “car” has the subcategories “tires” and “parts”. And the “parts” subcategory has the “engine” and “glass” subcategory. And the “engine” category has the post “Engine 1”.

    When I click on the category “car” the page needs to show only the subcategories “tires” and “parts”.

    When I click on the “parts” subcategory it should show the “engine” and “glass” subcategories

    Finally, when I click on the subcategory “engine” it should show posts related to “engine”.

    global $cat;
    $categoriasFilhas = get_categories(‘parent=’ . $cat);
    foreach($categoriasFilhas as $category) {
    echo ‘<p>term_id ) . ‘” >’ . $category->name.’</p>’;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    If the current category has no children $categoriasFilhas will be an empty array. Add a conditional for when it’s empty that makes a new WP_Query for posts in the current category. Loop through the results. See https://developer.www.ads-software.com/reference/classes/wp_query/

    BTW, the code you posted has syntax errors. The single quotes are not right. You apparently copy/pasted from elsewhere in the forums where the code was not demarcated with backticks. It also does not output anything clickable. The output is merely category names as text. It doesn’t really affect your question or my answer, but it doesn’t work as you say it does.

    Thread Starter ramponi

    (@ramponi)

    Sorry. The code correct is:

    global $cat;
    $categoriasFilhas = get_categories('parent=' . $cat);
    foreach($categoriasFilhas as $category) {
    echo '<p><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a></p>';
    }

    But WP_Query will show for me the posts of category?

    • This reply was modified 4 years, 6 months ago by ramponi.
    Thread Starter ramponi

    (@ramponi)

    I managed to get to this code below, but it is not filtering only posts from the selected category. He’s bringing them all.

    <?php 
    
    global $cat;
    $categoriasFilhas = get_categories('parent=' . $cat);
    foreach($categoriasFilhas as $category) {
    echo '<p><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a></p>';
    }
    
    $the_query = get_posts( array( 
    	'category_name' => $category->name,
    ) );
     
    
    if ( count( $the_query ) ) {
    	echo '<ul>';
    	foreach( $the_query as $novidade ) {
    		echo '<li>' . get_the_title( $novidade->ID ) . '</li>';
    	}
    	echo '</ul>';
    }
    
    ?>
    Moderator bcworkz

    (@bcworkz)

    Yes, get_posts() is sort of a wrapper for WP_Query class. They essentially do the same thing. They both accept the same arguments.

    The code you posted works as expected when used on my site. All I did extra is set $cat to a parent category ID of my site. I got a list of posts assigned the same sub-category as the one linked. If you were to follow the category link you should get an archive listing of the same posts.

    Post queries can be altered by other code, which can cause confusing behavior when the alteration is applied where it shouldn’t be. Sometimes examining the actual SQL query used will be an indication of why it doesn’t work as expected. To see the SQL we need to use the WP_Query version of your code:

    $categoriasFilhas = get_categories('parent=' . $cat);
    foreach($categoriasFilhas as $category) {
    echo '<p><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a></p>';
    }
    
    $the_query = new WP_Query( array(
    	'category_name' => $category->name,
    ) );
    
    echo $the_query->request;
    
    if ( count( $the_query->posts ) ) {
    	echo '<ul>';
    	foreach( $the_query->posts as $novidade ) {
    		echo '<li>' . get_the_title( $novidade->ID ) . '</li>';
    	}
    	echo '</ul>';
    }
    Thread Starter ramponi

    (@ramponi)

    On my website, the code shows all posts and not just the category.
    The code is not filtering by category. Look:

    SELECT SQL_CALC_FOUND_ROWS op_posts.ID FROM op_posts WHERE 1=1 AND op_posts.post_type = ‘post’ AND (op_posts.post_status = ‘publish’ OR op_posts.post_status = ‘private’) ORDER BY op_posts.post_date DESC LIMIT 0, 10

    Thread Starter ramponi

    (@ramponi)

    I got it with the code below. The only point is that he is showing the posts in the main category (father) as well and not only in the final category (child). Ideally, it should show only in the child category. Is it possible to adjust this?

    
    <?php 
    
    $page_object = get_queried_object();
    
    $categoriasFilhas = get_categories('parent=' . $cat);
    foreach($categoriasFilhas as $category) {
    echo '<p><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a></p>';
    }
    
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'cat' => $page_object->cat_ID,
        'posts_per_page' => 5,
    );
    
    $arr_posts = new WP_Query( $args );
     
    if ( $arr_posts->have_posts() ) :
     
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="entry-content">
    
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>
            </article>
       
    <?php
        endwhile;
    endif;
    
    ?>
    
    
    Moderator bcworkz

    (@bcworkz)

    The last code you posted is not the same as your previous example. This last one would return all posts because 'cat' => $page_object->cat_ID, is incorrect. Should be 'cat' => $page_object->term_id,

    To conditionally output posts only for categories with no children, attempt to get_categories() where the parent ID is the page’s category term ID. If an empty array is returned the category has no children and the post query and output can proceed.

    Thread Starter ramponi

    (@ramponi)

    bcworkz, thank you very much!

    Here is the code! Perfect!

    
    <?php 
    
    $page_object = get_queried_object();
    
    $categoriasFilhas = get_categories('parent=' . $cat);
    foreach($categoriasFilhas as $category) {
    echo '<p><a href="' . get_category_link( $category->term_id ) . '" >' . $category->name.'</a></p>';
    }
    
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'cat' => $page_object->term_id,
        'posts_per_page' => 5,
    
    );
    
    $arr_posts = new WP_Query( $args );
    
    function category_has_children( $term_id ){ 
        $children = get_term_children( $term_id, "category" );
        if(is_array($children)){
          return $children;
        } else {
          return false;
        }
    }
     
    if ( $arr_posts->have_posts() ) :
     
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
    
    <?php if( category_has_children( $cat ) == false) : ?>
    
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="entry-content">
    
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>
            </article>
    
    <? endif; ?>
    
       
    <?php
        endwhile;
    endif;
    
    ?>
    
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Category and subcategory’ is closed to new replies.