Site logo

Archived topic

Custom Post type on default archive - how to add css classes?

8 replies · Started by Simon on February 5, 2021

Viewing posts 1–9 of 9

Hey Guys,
I have a question about custom post types and the default archive page. Let me try to explain:

1. I created a custom post type "news", essentially a copy of the default posts.
2. I use this function, to display the custom post type on the default archive here:


add_action('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','news') );
  }
}

As you can see, there are custom post types (the 2 on the top) and "normal" WordPress posts displayed on the archive page.

How can I add the CSS classes for the column layout & masonry the custom posts displayed on this page? (It seems like the Generatepress CSS classes are missing on the custom post type divs and images)
I already checked out your help page on this, but it seems not to work for my case ;)

Thank you so much in advance, I really love this theme and appreciate the work you put in it!

Best Regards,
Simon

Hi Simon,

The news category archive page is actually having the masonry. It's not obvious right now because the contents are happen to be the almost the same in height.

As you can see, if I add some more content in the title of the first post, the masonry layout would show.
https://www.screencast.com/t/japM8DYt

Let me know :)

Hey Ying,
this was just the server-side cache.

Feel free to reload the page, and you should be able to see it.

Thank you by the way, you are really fast!

Thanks again Ying,
unfortunately, I already tried that.

The Code now looks like this:


add_action('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','news') );
  }
}

add_filter( 'generate_blog_columns','tu_news_columns' );
function tu_news_columns( $columns ) {
    if ( is_post_type_archive( 'news' ) ) {
        return true;
    }

    return $columns;
}

But the site still looks the same, and the CSS classes are not applied. I guess the problem is, that this is the default archive page... But I don't know how to fix it ;)

Hi there,

Try this instead:

add_filter( 'generate_blog_columns', function( $columns ) {
    if ( 'news' === get_post_type() && ! is_singular() ) {
        return true;
    }

    return $columns;
} );

Hey Tom,
thank you, worked instantly 🙏

Have a great day, stay safe and best regards from Germany 👍
Simon

Glad I could help. Thanks, you too! :)

Tom's code worked for me too, thank you. (Been browsing the forum for 2 days to find a solution that works, maybe you should add this to the page Ying linked above.)

This archived topic is closed to new replies.