Get all posts from custom taxonomy in Wordpress

后端 未结 4 586
广开言路
广开言路 2020-12-25 09:51

Is there a way to get all the posts from a taxonomy in Wordpress ?

In taxonomy.php, I have this code that gets the posts from the term related to the cu

4条回答
  •  一整个雨季
    2020-12-25 10:08

    $myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
    echo  $myterms[0]->name;
    

    With that you'd post the first item, yo can then create a foreach; loop:

    foreach ($myterms as $term) { ?>
        
  • name; ?>
  • That way you'd list them, if you want to post all of them, -my solution- create a normal wordpress loop inside the foreach one, but it has to have something like:

    foreach ($myterms as $term) :
    
    $args = array(
        'tax_query' => array(
            array(
                $term->slug
            )
        )
    );
    
    //  assigning variables to the loop
    global $wp_query;
    $wp_query = new WP_Query($args);
    
    // starting loop
    while ($wp_query->have_posts()) : $wp_query->the_post();
    
    the_title();
    blabla....
    
    endwhile;
    
    endforeach;
    

    I posted something very similar here.

提交回复
热议问题