简体   繁体   中英

Only show posts from a certain subcategory

Im using this php code on my page to show subcategories, and when click on certain subcategory should redirect to posts that have that subcategory:

<?php
wp_list_categories( array(
'taxonomy' => 'categorys',
'title_li' => '',
'child_of' => get_term_by( 'slug', 'osten', 'categorys' )->term_id,
) );
?>

I created taxonomy.php

I have 6 categories to filter these posts and each Category have its subcategories. SO i need to filter posts, when i click on subcategory to show me only posts on it.

I used this code inside taxonomy.php but its showing me all of posts and not filtering by clicked subcategory:

<?php
                            $post_type = 'ortemp';
                            // Get all the taxonomies for this post type
                            $taxonomies = get_object_taxonomies($post_type); // names (default)
                            foreach( $taxonomies as $taxonomy ) : 
                                // Gets every "category" (term) in this taxonomy to get the respective posts
                                $terms = get_terms( $taxonomy ); 
                                echo '<div id="activities_categories">';
                                foreach( $terms as $term ) :
                                    $customposts = new WP_Query(
                                    array(
                                        'posts_per_page' => -1,
                                        'tax_query' => array(
                                            array(
                                                'taxonomy' => $taxonomy,
                                                'terms' => $term->slug,
                                                'field' => 'slug',
                                                // 'category' => 'category-1'
                                            )
                                        )
                                    )
                                    );
                                    if( $customposts->have_posts() ):
                                        while( $customposts->have_posts() ) : $customposts->the_post();
                                            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
                                        endwhile;
                                    endif;
                                endforeach;
                                echo '</div>';
                            endforeach;
                            ?>

try these code to your 'taxonomy.php' file.

$currentTerm = get_queried_object();
$subTerms    = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );

if ( ! empty( $subTerms ) ) {
    echo '<div id="activities_categories">';
    foreach ( $subTerms as $subTerm ) :
        $customposts = new WP_Query(
            array(
                'posts_per_page' => - 1,
                'tax_query'      => array(
                    array(
                        'taxonomy' => $currentTerm->taxonomy,
                        'terms'    => $subTerm,
                        'field'    => 'term_id',
                    )
                )
            )
        );
        if ( $customposts->have_posts() ):
            while ( $customposts->have_posts() ) : $customposts->the_post();
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            endwhile;
        endif;
    endforeach;
    echo '</div>';
}

another example

$currentTerm = get_queried_object();
$subTerms    = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );

if ( ! empty( $subTerms ) ) {
    echo '<div id="activities_categories">';
    $customposts = new WP_Query(
        array(
            'posts_per_page' => - 1,
            'tax_query'      => array(
                array(
                    'taxonomy' => $currentTerm->taxonomy,
                    'field'    => 'term_id',
                    'terms'    => $subTerms,
                )
            )
        )
    );
    
    if ( $customposts->have_posts() ):
        while ( $customposts->have_posts() ) : $customposts->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        endwhile;
    endif;
    echo '</div>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM