首页 文章

如何在WordPress页面模板中显示分类法的帖子?

提问于
浏览
2

不确定之前是否曾经问过这个问题,但是我已经创建了一个"Newsroom" Pods,其中包含了Newsroom类别的自定义分类 . 新闻室类别有3个领域:新闻稿,媒体,其他 . 我有一个WordPress页面模板: taxonomy-newsroom_category.php

如果符合以下条件, taxonomy-newsroom_category.php 用于显示Pods帖子:

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

我现在的问题是我找不到显示帖子详细信息的方法: image(thumbnail)title(post title)date_published

我希望有人能帮帮忙 . 谢谢

这是我目前使用的代码:

<?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( );
        ?>

3 回答

  • 0

    看看https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters . 本文档详细介绍了如何使用自定义帖子类型和分类法进行查询 . 你的代码可能是这样的 .

    $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
    }
    
  • 0

    我也找到了解决方案...我有自定义帖子类型“研究”,我必须根据自定义分类法类别过滤它们 . 如果您想编写一个分类法类别的帖子,请尝试使用类似的东西:

    $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 代表我创建的分类法中的一个类别(值来自选择选项中的HTML代码), $args 是对数据库的一些查询和'market'是我的自定义分类法的slug, $wp_query 返回所有过滤器帖子

    Market Taxonomy in Custom Post Type Studien

    自定义帖子类型中自定义分类的屏幕截图 . 如你所见,我有两组 . 首先点击两个帖子,然后点击最后两个帖子中的第二个帖子 . 也许它会帮助你发挥想象力

  • 0

    来自wordpress文档:

    https://codex.wordpress.org/Custom_Taxonomies

    我们有以下,为分类学人

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

相关问题