$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Archive for custom taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Archive for custom taxonomy

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far