1
votes

I have a custom post type and I'm trying to display a category page so when you click on a standard link to a category, you will see all of the posts in that category... It seems simple, and I have looked at the Wordpress hierarchy, but I can't figure out which template files I need to create...

I currently have this page pulling through my custom fields: http://ideedev.co.uk/newseed/brand/ NOT looping through custom post types. The code is essentially this:

<?php
/**
 * Template name: Main Category Template
 */
?>

<?php get_header(); the_post(); ?>



<!-- Featured Image ===========================================  -->

<div class="image-test-container">

    <?php if ( has_post_thumbnail() ) {

    // Get the post thumbnail URL
    $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
  } else {

    ?>
    <style type="text/css">.featured-image{
    display:none;
    }</style>
    <?php
  } ?>


    <div id="absolute1111" style=" background: url(<?php echo $feat_image; ?>);">
        <div class="centerd1111">
            <h1><?php the_title(); ?></h1>
        </div>
    </div>  

</div>





<!-- Page Content ===========================================  -->

<div class="container">

    <div class="row clearfix">
    <div class="">

    </div>
    </div>

    <div class="row clearfix">
    <div class="level-two-intro-text">
        <p><?php the_field('intro_text'); ?></p>
    </div>
    <div class="level-two-sub-title block__title">
        <?php the_field('sub_title'); ?>
    </div>
    </div>

</div>





<!-- Areas ===========================================  -->

<div class="container">
    <div class="row clearfix">

        <?php if(get_field('areas')): ?>


                <?php while(has_sub_field('areas')): ?>


                    <div class="single-area-item six columns">
                        <p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
                        <h2> <?php the_sub_field('area_title'); ?> </h2> 
                        <p> <?php the_sub_field('area_info'); ?> <p>
                        <div class="area-button"><a href="<?php the_sub_field('button_target'); ?>" class="btn btn--dark-blue" role="button"><?php the_sub_field('button_text'); ?></a></div>
                    </div>


                <?php endwhile; ?>


        <?php endif; ?>

    </div>
</div>

However, When I click on the Portfolio button under each category, I would like it to display a page of that category...

I could make this work by creating a single template page for each category and looping through and display all posts within a specific category, but that seems like the wrong way - I'm thinking there should be one template to show the category I have just clicked on...

I think I'm getting a little confused to be honest. Thanks for looking :)

+++ EDIT +++

Here's the code I'm using for the custom post type...

register_post_type('portfolio', $args);

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

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array('slug' => 'category'),
);

register_taxonomy('portfolio-category', array('portfolio'), $args);
3
Hi... are you using the default category for custom post type too? or custom taxonomy?Ashkar
It's custom - it's called portfolio-category, so I could keep it separate from the blog and other items I have...Shaun Taylor
I'll add the CPT text to the question if that helps...Shaun Taylor
Ok so you first want to get the custom-category link on the button and then you want to configure that category page template?Ahmed Ginani
Please see my answerAshkar

3 Answers

0
votes

You can add an archive template for your custom categories. The easiest way is to copy archive.php in your theme folder, and give it the correct name.

According to the template heirarchy (https://developer.wordpress.org/themes/basics/template-hierarchy/), you'll need to use the filename 'taxonomy-portfolio-category.php'

If you want to style each category differently, you can create files with the name "taxonomy-portfolio-category-{categoryslug}.php"

0
votes

Create a file 'taxonomy-portfolio-category.php'

your custom taxonomy code put here.

0
votes

Hi first create a page named taxonomy-portfolio-category.php and place below code , it is a sample code, you can edit it according to your theme

    <?php while ( have_posts() ) : the_post(); ?>

<?php /* How to display posts of the Gallery format. The gallery category is the old way. */ ?>
<div class="test">
<?php 
echo "<h2>".get_the_title()."</h2>";
echo "<div>".get_the_excerpt()."</div>";
?>
</div>
<?php endwhile; // End the loop. Whew. ?>