【问题标题】:WordPress: Taxonomy archives based on Custom Post TypeWordPress:基于自定义帖子类型的分类档案
【发布时间】:2016-11-10 14:52:20
【问题描述】:

我生成了三种不同的自定义帖子类型(例如书籍、电影、游戏)。 而且我对所有这些都有一个自定义分类法(例如流派)。

我需要的是基于帖子类型的分类档案。 例如:“书籍-流派”、“电影-流派”...

有什么解决办法吗?现在我只有“流派”的分类档案。

【问题讨论】:

  • 因此您希望所有分类档案都包含三个部分(书籍、电影、游戏)。
  • 不完全是。这就是我已经做过的。我想要每个帖子类型的分类档案。例如:只有科幻书。我想为这些档案拥有自己的 URL,例如:/book-genre/science-fiction/
  • 好的,那么您只需要带有永久链接修改的自定义帖子类型存档。那么这实际上是一个两部分的问题:您需要帮助设置自定义帖子类型存档还是帮助设置永久链接结构?
  • 我认为首先是关于自定义帖子类型档案
  • 我知道如何生成自定义帖子类型档案。问题在于与分类法的结合

标签: wordpress custom-post-type taxonomy custom-taxonomy


【解决方案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();
    }
  ?>

【讨论】:

  • 谢谢!但是我已经通过自定义页面模板上的自定义查询解决了这个问题。我现在的问题是分类档案仅基于自定义帖子类型
  • 分类档案不能这样工作。如果它是带有特定分类术语标记的自定义帖子类型的存档,您需要使用我在上面在 CPT 存档中显示的循环。
  • 好的,谢谢。但是怎么可能链接到分类法呢?
猜你喜欢
  • 2011-06-04
  • 1970-01-01
  • 2011-04-17
  • 2016-08-26
  • 2013-06-22
  • 2018-06-15
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
相关资源
最近更新 更多