Site logo

Archived topic

Columns in Custom Post Type Archives

5 replies · Started by wekhter on October 16, 2016

Viewing posts 1–6 of 6

I found code that allows masonry to be enabled on CPT archives (this code) but can't seem to find any (working) code to enable columns on CPT archives.

I want to have masonry enabled for the actual blog archive, but 3 columns enabled for CPT archives.

Also, is it possible to make the featured image that shows up in column layouts to be the wordpress-generated square thumbnail? It's nice to have different image proportions in masonry view, but for post grids I'd like the images to be more cohesive.

You need to disable masonry in order for columns to work.

So you would do this:

add_filter( 'generate_blog_masonry','generate_adjust_masonry' );
function generate_adjust_masonry( $masonry )
{
    if ( is_archive() && ! is_post_type_archive( 'portfolio' ) ) :
        return 'false';
    endif;

    return $masonry;
}

add_filter( 'generate_blog_columns','generate_adjust_columns' );
function generate_adjust_columns( $columns )
{
    if ( is_post_type_archive( 'portfolio' ) ) :
        return true;
    endif;

    return $columns;
}

Not 100% sure that will work, but worth a shot.

Let me think about the image thing while you try the above. Can't come up with anything right now that will do that.

Thanks! Code wasn't quite there but I was able to get it working for my needs (my CPT is not portfolio and I need the changes to affect taxonomy term archives as well)

add_filter( 'generate_blog_masonry','generate_adjust_masonry' );
function generate_adjust_masonry( $masonry )
{
    if ( is_post_type_archive() || is_tax() ) :
        return 'false';
    endif;

    return $masonry;
}

add_filter( 'generate_blog_columns','generate_adjust_columns' );
function generate_adjust_columns( $columns )
{
    if ( is_post_type_archive() || is_tax() ) :
        return true;
    endif;

    return $columns;
}

Any input on the image size thing would be great :)

Perfect.

Ok, image sizes..

So, there's a filter you can use to change image sizes depending on conditionals - it looks like you can fill in the conditionals:

add_filter( 'generate_blog_image_attributes','tu_variable_image_sizes' );
function tu_variable_image_sizes( $atts )
{
    // Set up our conditional
    if ( is_post_type_archive() || is_tax() ) :
        $atts[ 'width' ] = 300;
        $atts[ 'height' ] = 300;
        $atts[ 'crop' ] = true;
    endif;

    // Return our options
    return $atts;
}

Now I'm not sure if we can filter it depending on using columns or masonry, but if we could, we would use these conditionals:

if ( generate_blog_get_columns() ) :
    // Columns are active
endif;

if ( 'true' == generate_blog_get_masonry() ) :
    // Masonry is active
endif;

Beautiful! The first code did exactly what I wanted.

Here's another question--slightly related but slightly not.

Part of customizing the layouts for these CPT archives is putting together stuff for the individual CPT pages as well.

I was able to set the layout of these CPT pages using the code on this page (https://generatepress.com/knowledgebase/choosing-sidebar-layouts/) but I'd like to be able to set which page elements are set/enabled by default as well. As in, I'd like the secondary navigation to be only visible on these CPT pages and default disabled everywhere else. It should be disabled by default because otherwise it shows up on pages I have no control over otherwise (like blog archives, Woocommerce pages, etc)

Any filters for that?

No filters, but you can do that with CSS.

Your custom post types will add unique classes to the element.

So you can hide the secondary nav:

.secondary-navigation {
    display: none;
}

Then use the unique class to display it:

.unique-cpt-class .secondary-navigation {
    display: block;
}
This archived topic is closed to new replies.