0

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.com/series does not work I get my 404 page, but if I were to try and find the specific series let's say siteurl.com/series/test-series this does find the page.

2) When I try siteurl.com/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(); ?>
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. 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

1 Answer 1

3

I noticed that siteurl.com/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.com/category/ returns a 404, even if you have categories.

When I try siteurl.com/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.com/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(); ?>
1
  • Thank you, I actually didn't know about not needing the custom query at whenever I added that I had removed my loop in what I can only assume was late night delusions. Much appreciated Jacob. Commented Feb 13, 2019 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.