How to display posts of taxonomy in WordPress page

2019-07-15 08:38发布

Not sure if this has been asked before, but I'm a bit lost. I've created a "Newsroom" Pods with a custom taxonomy of Newsroom Category. Newsroom Category has 3 fields: Press Release, Media, Others. I have a WordPress page template: taxonomy-newsroom_category.php

taxonomy-newsroom_category.php is used to display Pods posts if meets the following:

1 - pods = 'newsroom'
2 - taxonomy = 'press_release' || 'media' || 'others'

My issue right now is that I can't find a way to display the post details: image(thumbnail), title(post title), date_published

I hope someone can help. Thanks

Here's the code I'm currently using:

<?php
                //Setup Pod object
                //Presuming permalink structure of example.com/pod-name/item-name
      //See http://pods.io/code/pods/find
                //Set $params to get 5 items

                $params = array(
          'limit' => 5,
        );

        //get current pod name
        $pod_name = pods_v( 0, 'newsroom');
        //get pods object
        $pods = pods( $pod_name, $params );

        //check that total values (given limit) returned is greater than zero
        if ( $pods->total() > 0 ) {
          //loop through items using pods::fetch
          while ($pods->fetch() ) {
            //Put title/ permalink into variables
            $post_title = $pods->display('post_title');
            $date_published = $pods->display('date_published');
            $permalink = site_url( trailingslashit( $pod_name ) . $pods->field('permalink') );
        ?>
                        <div class="news-item col-sm-4">
                            <div class="news-item-img"></div>
                            <div class="news-item-header">
                                <h5 class="news-category"></h5>
                                <h2 class="news-item-title"><a href="<?php echo $permalink; ?>"><?php echo $post_title; ?></a></h2>
                                <h5 class="news-item-date"><?php echo $date_published; ?></h5>
                            </div>
                        </div><!-- close -->
                <?php
          } //endwhile;
        } //endif;

        // Output Pagination
        //see http://pods.io/docs/code/pods/pagination
        echo $pods->pagination( );
        ?>

标签: php wordpress
3条回答
做自己的国王
2楼-- · 2019-07-15 08:47

I also found out solution for that... I had my Custom Post Type "studies" and I had to filter them, based on custom taxonomy category. If you want to write Posts of one taxonomy category, try to use something like that:

$type = $_GET['type'];

$args = array(
        "post_type" => "studien",
        "post_per_page" => -1,
        "relation" => "AND"
);


if($type != "") {
    $args['tax_query'][] = array(

        'taxonomy' => 'market',
        'field' => 'slug',
        'terms' => $type
    );

 $wp_query = new WP_Query($args);

}

$type represents one category in my created Taxonomy (value comes from HTML code in select option), $args Is some query on database and 'market' is slug of my custom Taxonomy, $wp_query returns all filter Posts

Market Taxonomy in Custom Post Type Studien

screenshot of my custom taxonomy in custom post type. As you can see, I have two groups. First is clicked in two Posts and Second is clicked in last two Posts. Maybe it will helps you to give imagination

查看更多
▲ chillily
3楼-- · 2019-07-15 08:48

Check this out https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters. This document explained how to query with custom post type and taxonomy in details. Youd code could be something like this.

$args = array(
  'post_type' => 'newsroom',
  'tax_query' => array(
      array(
        'taxonomy' => 'newsroom_category',
        'field'    => 'slug',
        'terms'    => 'press_release',
      ),
  ),
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $id = get_the_ID(); // with post id, you can get whatever you want.
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-07-15 09:10

from the wordpress documenation:

https://codex.wordpress.org/Custom_Taxonomies

we have the below , for taxonomy person

$args = array( 'tax_query' => array( array( 'taxonomy' => 'person', 'field' => 'slug', 'terms' => 'bob' ) ) ); $query = new WP_Query( $args );

查看更多
登录 后发表回答