Site logo

Archived topic

Page Header for archive page with custom posts

9 replies · Started by Roger on July 9, 2019

Viewing posts 1–10 of 10

Hi, I've created a custom post type of 'hunt' using the following code:

if ( ! function_exists( 'create_post_type' ) ) :

function create_post_type() {
    
    register_post_type( 'hunt', 
        array(
            'labels' => array(
                'name' => __( 'Hunts' ), 
                'singular_name' => __( 'Hunt' ), 
            ),
            'public' => true,
            'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail', 'excerpt' ), // do you need all of these options?
            'taxonomies' => array( 'category', 'post_tag' ), 
            'hierarchical' => true,
            'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
            'rewrite' => array ( 'slug' => __( 'hunts' ) ) 
        )
    );

}
add_action( 'init', 'create_post_type' );

endif;

And I've added the following filter so that this custom post type appears on the category page along with standard posts:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if( is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item', 'post', 'hunt');
    $query->set('post_type',$post_type);
    return $query;
    }
}

However, I've now lost the styling for the archive page that I set in the customizer and, the header element isn't being applied to the custom post type. What am I doing wrong?

Hi Tom,

1. Yes - the column settings that I've applied to the category page for standard posts and that I want to apply to the category page for my custom posts are as follows:

column settings

I've added the filter that you suggest but nothing has changed.

2. Yes, I've set it to display on the category archive for the custom post, but nothing is appearing.

Element display settings

1. That filter is needed. Did you update the name of the cpt to look for?

2. Can you try "Hunt Category Archive" instead? That Display Rule will target single posts inside that category.

Hi Tom,

1. Yes, I updated the cpt name, but it's not working

2. I've changed the display location for the element to Hunt Category Archive, but still no joy.

I noticed that although in the Element itself it shows that 'Hunt Category Archive' has been selected, on the summary page for all Elements it shows 'Post Category Archive' which might suggest some sort of conflict/error.

Display Location in the Element itself:

All Elements list view:
null

So it looks like you're using a regular category taxonomy. What happens if you choose "Vancouver" along with "Post Category Archive"?

Just gave that a try. Changing 'Hunt Category Archive' to 'Post Category Archive' in the Header Element fails to display the Header too.

Hmm, what is happening here..

What if you create a custom taxonomy for that CPT instead of using post categories?

Ok. I created a custom taxonomy of 'Cities' for that CPT and set the Header Element to display for 'Hunt City Archive - All Cities' and we have lift off! Not sure why Elements didn't like the combined taxonomy, but this will be fine.

I also adjusted the column filter and that's all sorted too:

add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
    if ( is_tax() ) {
        return true;
    }

    return $columns;
}

Thanks for help troubleshooting :)

Strange, I'll look into it.

Glad it's all working now :)

This archived topic is closed to new replies.