PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to display child of custom taxonomy in a loop?

 July 08, 2022     custom-taxonomy, loops, posts, taxonomy, wordpress     No comments   

Issue

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'
                    )
                )
            ); 



Answered By - Ricky
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing