Black Friday! Get up to 25% off GP Premium! Learn more ➝

[Resolved] Custom Post type on default archive – how to add css classes?

Home Forums Support [Resolved] Custom Post type on default archive – how to add css classes?

Home Forums Support Custom Post type on default archive – how to add css classes?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1647535
    Simon

    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

    #1647554
    Ying
    Staff
    Customer Support

    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 πŸ™‚

    #1647567
    Simon

    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!

    #1647607
    Ying
    Staff
    Customer Support
    #1647894
    Simon

    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 πŸ˜‰

    #1648415
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try this instead:

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

    Hey Tom,
    thank you, worked instantly πŸ™

    Have a great day, stay safe and best regards from Germany πŸ‘
    Simon

    #1649443
    Tom
    Lead Developer
    Lead Developer

    Glad I could help. Thanks, you too! πŸ™‚

    #1942857
    Annamari

    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.)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.