Archive for custom taxonomy

时间:2019-02-20 作者:RITIK

我在函数中定义了一个自定义的post类型电影。php

function create_post_type() {
  register_post_type( \'movies\',
  array(
    \'supports\' => array(\'title\', \'editor\' , \'excerpt\' , \'custom-fields\'),
    \'labels\' => array( \'taxonomies\'  => \'mvgen\',
      \'name\' => __( \'movies\' ),
      \'singular_name\' => __( \'movies\' ),
    ),
    \'public\' => true,
    \'has_archive\' => true,
  )   
  ); 
} 
add_action( \'init\', \'create_post_type\' );
现在归档页和单页工作正常。但我为这种帖子类型定义了两种自定义分类法,例如“戏剧”和“摇滚”

我想当用户点击这个分类法时,他们会得到所有与这个特定分类法相关的帖子
为此,我创建了页面taxonomy-mvgen-drama.php复制如下:

  <?php get_header(); ?>
  <section class="blog_sect">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <?php the_title(); ?>
      <?php the_content(); ?>
      <?php echo get_the_date(); ?>
      <p>CATEGORY: <?php the_category(\', \'); ?></p>
      </p>    

      <!-- ----------------printing taxonomy for a specifuic post------------------ -->
      <?php

      ?>
      <br>
      <p><?php echo "LANGUAGES :"." ".get_the_term_list($post->ID, \'mvgen\', \'\' , \' , \') ?></p> 

      </b>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php get_footer(); ?>
  </section>
  <?php get_footer(); ?> 
但每当我点击这个分类法时,它不会带我去分类法mvgen戏剧,而是带我去用url索引页面http://localhost/movies/mvgen/rock/

你能帮我做这个吗?

2 个回复
SO网友:MikeNGarrett

您还没有在这里发布注册分类法的代码,但我假设这是可行的。在创建自定义帖子类型的代码中,标签数组中包含分类法。这需要处于顶层。此外,分类法的值需要是一个数组,即使其中只有1项。见下文。More information.

function create_post_type() {
  register_post_type( \'movies\',
  array(
    \'supports\' => array(\'title\', \'editor\' , \'excerpt\' , \'custom-fields\'),
    \'taxonomies\'  => array( \'mvgen\' ),
    \'labels\' => array(
      \'name\' => __( \'movies\' ),
      \'singular_name\' => __( \'movies\' ),
    ),
    \'public\' => true,
    \'has_archive\' => true,
  )   
  ); 
} 
add_action( \'init\', \'create_post_type\' );
您的模板名称在我看来是正确的,但您始终可以引用template hierarchy 以确保目标文件名正确。

此外,我发现Show Current Template plugin 在尝试确定正在使用的文件时提供帮助。

SO网友:Tanmay Patel

根据Wordpress Codex 第页,共页Template Hierarchy, 创建一个名为的模板文件taxonomy-mvgen.php. WordPress将使用它来显示该分类法的存档。for example "DRAMA" and "Rock.“您也可以使用taxonomy-mvgen-drama.php 和至taxonomy-mvgen-rock.php 为分类法中的特定术语创建模板。

相关推荐

outputting taxonomy hierarchy

我有一个网站,其中列出了侧导航的子类别列表。我需要将这些子类别划分为更具体的层次结构。它当前正在从父类别输出一个列表,但不显示超出该类别的层次结构。我有点被困在下一步了。这就是我现在所拥有的: foreach($subcategory as $key => $val) { $args = array( \'posts_per_page\' => -1, \'post_type\' => \'storefront\',