简体   繁体   中英

How to display posts using custom texonomy in wordpress?

I have installed Wp-Types plugin in my wordpress blog. I create a custom taxonomy called 'demo'. In which I have added two categories.

  • firstdemo
  • seconddemo

Now I want to show posts from this custom taxonomy so I am using this:

<?php


$custom_terms = get_terms('demo');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'demo',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        endwhile;
     }
}


?>

But its showing all posts who are using that taxonomy. I want to show posts from seconddemo. how to do it?

In your array you are missing 'terms' => array('seconddemo')

<?php


$custom_terms = get_terms('demo');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'demo',
                'field' => 'slug',
                'terms' => array('seconddemo'),
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        endwhile;
     }
}


?>

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