Archive for custom taxonomy

admin2025-06-03  0

I have a custom post type movies that I have defined in function.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' );

Now the archive page and single page are working good. But I have defined two custom taxonomies for this post type, for example "DRAMA" and "Rock."

I want when user click on this taxonomy they get all post related to that particular taxonomy.
For that I have created page taxonomy-mvgen-drama.php which is copied below:

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

But whenever I click on this taxonomy it doesn't take me to taxonomy-mvgen-drama instead it takes me to index page with the url http://localhost/movies/mvgen/rock/

Can you help me with this?

I have a custom post type movies that I have defined in function.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' );

Now the archive page and single page are working good. But I have defined two custom taxonomies for this post type, for example "DRAMA" and "Rock."

I want when user click on this taxonomy they get all post related to that particular taxonomy.
For that I have created page taxonomy-mvgen-drama.php which is copied below:

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

But whenever I click on this taxonomy it doesn't take me to taxonomy-mvgen-drama instead it takes me to index page with the url http://localhost/movies/mvgen/rock/

Can you help me with this?

Share Improve this question edited Feb 20, 2019 at 18:44 MikeNGarrett 2,6811 gold badge20 silver badges29 bronze badges asked Feb 20, 2019 at 17:24 RITIKRITIK 1011 bronze badge 1
  • And how are these taxonomies registered? – Krzysiek Dróżdż Commented Feb 20, 2019 at 19:50
Add a comment  | 

2 Answers 2

Reset to default 0

You haven't posted here the code for registering a taxonomy, but I'm going to assume that's working. In your code for creating the custom post type, you have the taxonomies inside the labels array. This needs to be at the top level. Additionally, the value of taxonomies needs to be an array, even if it only has 1 item in it. See below. 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' );

Your template name looks correct to me, but you can always reference the template hierarchy to make sure you're targeting the correct file name.

In addition, I find the Show Current Template plugin helps when trying to determine what file is being used.

According to the Wordpress Codex page on Template Hierarchy, you create a template file with the name of taxonomy-mvgen.php. WordPress will use that to display the archive for that taxonomy. for example "DRAMA" and "Rock." You can also use taxonomy-mvgen-drama.php and to taxonomy-mvgen-rock.php create templates for specific terms in your taxonomy.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748882368a314508.html

最新回复(0)