questions

How to display child of custom taxonomy in a loop?

Rate this post

I have created one taxonomy for posts, topics

 

add_action( 'init', 'create_cw_hierarchical_taxonomy', 0 );
//create a custom taxonomy name
function create_cw_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => __( 'Parent Topic' ),
'parent_item_colon' => __( 'Parent Topic:' ),
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'menu_name' => __( 'Topics' ),
);
// taxonomy register
register_taxonomy('topics',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}

 

Now I have created two topics in backend, topic 1 and topic 2

I want to display both the topics in different loops, so I have written the following codes

 

<?php
            $options = array(
                'post_type' => 'post',
                'posts_per_page' => 1,
                'orderby' => 'date',
                'order' => 'ASC',
                'paged' => $paged,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'topics',
                        'field' => 'name',
                        'terms' => 'topic-1'
                    )
                )
            ); ?>
            
            <?php $query = new WP_Query( $options );
            if ( $query->have_posts() ) :
                while($query->have_posts()) : $query->the_post();?> 

            <div class="col-sm-8">
                <div class="featured-post-large m-m-b-1">
                    <div class="featured-post-large-control text-light">
                        <div class="featured-post-large-content mx-5">
                            <div><?php the_category( $separator, $parents, $post_id ); ?></div>
                            <h2><a href="<?php echo get_permalink($post->ID) ?>" class="archive-title"><?php the_title()?></a></h2>
                            <div class="py-2"><?php the_excerpt()?></div>
                        </div>
                    </div>
                </div>
            </div>
            
            <?php endwhile; wp_reset_query();
            endif; ?>

 

I am not able to get the posts written under topic 1 and topic 2, please guide me on this. thanks!

 

✔️Solution:

$options = array(
                'post_type' => 'post',
                'posts_per_page' => 1,
                'orderby' => 'date',
                'order' => 'ASC',
                'paged' => $paged,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'topics',
                        'field' => 'slug',
                        'terms' => 'topic-1'
                    )
                )
            ); 

Leave a Reply

Your email address will not be published.

Back to top button