首页 文章

WordPress:基于自定义帖子类型的分类法归档

提问于
浏览
0

我已经生成了三种不同的自定义帖子类型(例如书籍,电影,游戏) . 我为他们所有人(例如流派)制定了自定义分类法 .

我需要的是基于帖子类型的taxanomy档案 . 例如:“books-genre”,“movies-genre”......

有没有解决办法呢?现在我只有“流派”的分类档案 .

1 回答

  • 1

    我喜欢接近自定义帖子存档的方法是创建一个自定义存档模板,其中包含我需要的 WP_Query 部分 . 您可以在 archive-cptnamehere.php 的主题根目录中创建空白文件 .

    您可能需要添加一些模板部分,但页面的核心如下所示:

    <?php
        // 1- Get ID of the page's path by URL (reliable)
        $pagePath = $_SERVER['REQUEST_URI'];
        $pageObject = get_page_by_path($pagePath);
        $pagePathID = $pageObject->ID;
    
        // 2- Print page title
        $teamPageTitle = get_the_title($pagePathID);
        echo $teamPageTitle;
    
        // 3 - Do a query that gets the data you need
        // Args: -1 shows all locations, orders locations alphabetically by title, orders locations a-z, only query against team items
        $args = array(
          'posts_per_page' => -1,
          'orderby' => 'title',
          'order' => 'ASC',
          'post_type' => 'team',
          'meta_query'  => array(
            array(
             'key'          => 'team_page_categorization',
             'value'        => $team_page_categorization_options_array_item,
             'compare'  => 'LIKE'
            )
          )
        );
        $the_query = new WP_Query( $args );
    
        // 4- Setup query while loop
        if($the_query->have_posts()) {
          while($the_query->have_posts()) {
            $the_query->the_post();
    
            // 5- Do what you need to inside the loop
    
    
          // 6- Close it all up, don't forget to reset_postdata so you can do additional queries if necessary!    
          }
         wp_reset_postdata();
        }
      ?>
    

相关问题