0
votes

I'm having issues viewing all posts in a category.

I have created a custom post and categories to go with it in the functions.php, like so:

function create_posttype() {
    register_post_type( 'tours',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Tours' ),
                'singular_name' => __( 'Tour' )
            ),
            'public' => true,
            //'has_archive' => true,
            'rewrite' => array('slug' => 'tours'),
            //'taxonomies' => array('category'), //A added
            'hierarchical' => true //A added
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

function my_taxonomies_tours() {
  $labels = array(
    'name'              => _x( 'Tour Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Tour Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Tour Categories' ),
    'all_items'         => __( 'All Tour Categories' ),
    'parent_item'       => __( 'Parent Tour Category' ),
    'parent_item_colon' => __( 'Parent Tour Category:' ),
    'edit_item'         => __( 'Edit Tour Category' ), 
    'update_item'       => __( 'Update Tour Category' ),
    'add_new_item'      => __( 'Add New Tour Category' ),
    'new_item_name'     => __( 'New Tour Category' ),
    'menu_name'         => __( 'Tour Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'tour_category', 'tours', $args );
}

add_action( 'init', 'my_taxonomies_tours', 0 );

I have created a template to display all categories in the hopes you can click on a category and have it list all the posts. In my template I have the following:

$custom_terms = get_terms('tour_category');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'tours',
        'tax_query' => array(
            array(
                'taxonomy' => 'tour_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

     }
}

What I have currently lists the categories, but I don't know how to link it in such a way so you can view all posts within that particular category. Any help would be greatly appreciated. Using Wordpress 4.4

1

1 Answers

0
votes

Try adjusting your $args variable like this (add posts_per_page):

$args = array('post_type' => 'tours',
        'tax_query' => array(
            array(
                'taxonomy' => 'tour_category',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
        'posts_per_page' => -1,
     );

Otherwise it use default posts per page value.