WordPress Custom Post type archive display

I have a custom post type named STORIES.
Within that post type, I have categories, so each STORY is also assigned a category like this…

Story 1 (assigned category RED)
Story 2 (assigned category BLUE)
Story 3 (assigned category GREEN) etc.

Read More

I need to display a page which lists the categories, and when clicked, then lists the posts from that category.

So the page would be a list with…

RED
BLUE
GREEN

What template would control this list?

Then I click on RED and it takes me to a template that displays only the RED Stories.

What template would control this list?

I am guessing I need three specific templates?
One called template-stories.php and one called archive-stores.php and a final one called single-stories.php?

Related posts

1 comment

  1. you are guessing right, first you need a page template where you are going to call your terms, but you need to call by his taxonomy, right?, To do that, you need to use the function get_terms( $taxonomies, $args );, and with the function get_term_link($term), you are going to obtain the url of the current term:

    $args = array( 'hide_empty=0' );
    
    $terms = get_terms( 'my_term', $args );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
       $count = count( $terms );
       $i = 0;
       $term_list = '<p class="my_term-archive">';
       foreach ( $terms as $term ) {
          $i++;
          $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
        if ( $count != $i ) {
            $term_list .= ' &middot; ';
        }
        else {
            $term_list .= '</p>';
        }
    }
    echo $term_list;
    

    }

    Then, you need the archive.php where you are going to display all posts associated to this current term.

    But first you need to get the current term, and you can do it using the function get_term_by($field, $value, $taxonomy, $output, $filter).

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    

    Now you can make an if statement:

    if($term = 'red'){
        the_title();
        //Or get_template_part( 'content', 'stories' ); 
    }else{
      //...
    }
    

Comments are closed.