Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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;
    
    ?>
    
    
    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;
    
    ?>
    
    
    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)

    Hi,

    Ok. Sorry.

    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>';
    }
    
    ?>
    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.
Viewing 6 replies - 1 through 6 (of 6 total)