$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'); ?>wp query - Custom taxonomy.php not working|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)

wp query - Custom taxonomy.php not working

matteradmin9PV0评论

I have created a custom taxonomy called "series" (code below) and two pages: taxonomy.php and a taxonomy-series.php

1)I noticed that siteurl/series does not work I get my 404 page, but if I were to try and find the specific series let's say siteurl/series/test-series this does find the page.

2) When I try siteurl/series/test-series and it works it is only pulling one post but in the series even though it should be displaying all of them. I have provided code to my taxonomy-series.php in case that helps us find the solution.

Please let me know what steps I've missed since this is my first time I assume I made a mistake along the way and after two weeks troubleshooting I am at a loss. I appreciate any help!

1) functions.php Register Custom Taxonomy (Series) Code

function create_series_hierarchical_taxonomy() {
   $labels = array(
    'name' => _x( 'Series', 'taxonomy general name' ),
    'singular_name' => _x( 'Series', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Series' ),
    'all_items' => __( 'All Series' ),
    'parent_item' => __( 'Parent Series' ),
    'parent_item_colon' => __( 'Parent Series:' ),
    'edit_item' => __( 'Edit Series' ), 
    'update_item' => __( 'Update Series' ),
    'add_new_item' => __( 'Add New Series' ),
    'new_item_name' => __( 'New Series Name' ),
    'menu_name' => __( 'Series' ),
  );    

  $args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'taxonomies' => array( 'series' ),
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'series', 'with_front' => true, 'hierarchical' => true ),
    'show_in_rest' => true,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
  );

  register_taxonomy('series', array('post'), $args );

}

2) taxonomy-series.php

<?php get_header(); ?>
    <div class="container">
        <div class="row">  
          <h2><?php single_cat_title(); ?></h2>    

    <?php
        $args = array(
            'numberposts' => -1,
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'series',
                    'field'    => 'slug',
                ),
            ),
        );
        $query = new WP_Query( $args );
    ?>
            <div class="col-sm-6 card-block-img">
                <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
                <a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>"></a>
            </div>

            <div class="col-sm-6>
              <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            </div>
        </div><!-- END ROW -->
    </div><!-- END CONTAINER -->
<?php get_footer(); ?>

I have created a custom taxonomy called "series" (code below) and two pages: taxonomy.php and a taxonomy-series.php

1)I noticed that siteurl/series does not work I get my 404 page, but if I were to try and find the specific series let's say siteurl/series/test-series this does find the page.

2) When I try siteurl/series/test-series and it works it is only pulling one post but in the series even though it should be displaying all of them. I have provided code to my taxonomy-series.php in case that helps us find the solution.

Please let me know what steps I've missed since this is my first time I assume I made a mistake along the way and after two weeks troubleshooting I am at a loss. I appreciate any help!

1) functions.php Register Custom Taxonomy (Series) Code

function create_series_hierarchical_taxonomy() {
   $labels = array(
    'name' => _x( 'Series', 'taxonomy general name' ),
    'singular_name' => _x( 'Series', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Series' ),
    'all_items' => __( 'All Series' ),
    'parent_item' => __( 'Parent Series' ),
    'parent_item_colon' => __( 'Parent Series:' ),
    'edit_item' => __( 'Edit Series' ), 
    'update_item' => __( 'Update Series' ),
    'add_new_item' => __( 'Add New Series' ),
    'new_item_name' => __( 'New Series Name' ),
    'menu_name' => __( 'Series' ),
  );    

  $args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'taxonomies' => array( 'series' ),
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'series', 'with_front' => true, 'hierarchical' => true ),
    'show_in_rest' => true,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
  );

  register_taxonomy('series', array('post'), $args );

}

2) taxonomy-series.php

<?php get_header(); ?>
    <div class="container">
        <div class="row">  
          <h2><?php single_cat_title(); ?></h2>    

    <?php
        $args = array(
            'numberposts' => -1,
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'series',
                    'field'    => 'slug',
                ),
            ),
        );
        $query = new WP_Query( $args );
    ?>
            <div class="col-sm-6 card-block-img">
                <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
                <a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>"></a>
            </div>

            <div class="col-sm-6>
              <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            </div>
        </div><!-- END ROW -->
    </div><!-- END CONTAINER -->
<?php get_footer(); ?>
Share Improve this question asked Feb 13, 2019 at 0:25 user5854648user5854648 16317 bronze badges 2
  • at a glance you are entirely missing the loop after making the query: wrap the col-sm-6 div inside a <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> and close the while (<?php endwhile; ?>) after you close the column. About the front page, look at the answers in this questions. – Fabrizio Mele Commented Feb 13, 2019 at 0:37
  • 1 Why are you creating a new query in your template? Just run the normal loop, WordPress generates the main query for you. – Milo Commented Feb 13, 2019 at 1:13
Add a comment  | 

1 Answer 1

Reset to default 3

I noticed that siteurl/series does not work I get my 404 page

This is the correct behaviour. There is no content to display at this URL in WordPress. You need to provide a specific term so that WordPress can retrieve posts with that term.

Notice that in a fresh install of WordPress siteurl/category/ returns a 404, even if you have categories.

When I try siteurl/series/test-series and it works it is only pulling one post but in the series even though it should be displaying all of them.

The problem is likely that you have not built taxonomy-series.php correctly.

The first problem is that even though you've performed a query with WP_Query, you aren't actually looping over the the results to output each one. Your code is written to only output once. Regardless, you shouldn't be using WP_Query anyway.

When viewing a series at siteurl/series/test-series, WordPress will automatically query the correct posts for you. To display them you shouldn't be making your own query. You should use The Loop.

So taxonomy-series.php should look like this:

<?php get_header(); ?>
    <div class="container">
        <div class="row">  
            <h2><?php single_cat_title(); ?></h2>    

            <?php while ( have_posts() ) : the_post(); ?>
                <div class="col-sm-6 card-block-img">
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'single-post-thumbnail' ); ?>
                    <a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>"></a>
                </div>

                <div class="col-sm-6">
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                </div>
            <?php endwhile; ?>
        </div><!-- END ROW -->
    </div><!-- END CONTAINER -->
<?php get_footer(); ?>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far