Site logo

Archived topic

Archives with no side bar: 3 columns. Archives with sidebar: 2 columns

5 replies · Started by Bandara on April 21, 2021

Viewing posts 1–6 of 6

Hello!

Generally I like the look of the three column archive page with no sidebars. So that's what I'm trying to optimize for.

However on the archive page for a custom post type (using a "series" custom taxonomy) I would like to have the right sidebar. I can do that no problem. But three columns plus side bar is too much.

So is there a way to make it so any time there is a sidebar (I only plan on using the right sidebar) the archive page switches to two columns? I've looked at the layout element but I'm not seeing a place that sets number of columns.

The way I have removed the sidebar for the archive pages is with a Layout Element using content only (no sidebar) with the location "All Archives" and an Exclude For the CPT archive.

In the customizer I have set the blog archive to be three columns.

If there was a universal solution to set this rule that would be great. However I am also open to adding to php each time I need to make an archive two columns.

Hi there,

you can use the generate_blog_get_column_count filter - heres an example of how its used:

https://docs.generatepress.com/article/using-columns-in-the-blog/#changing-the-number-of-columns

In your case you would do something like this:

add_filter( 'generate_blog_get_column_count','tu_search_column_count' );
function tu_search_column_count( $count ) {
    if ( 'your-cpt-slug' === get_post_type() && ! is_singular() ) {
        return 50;
    }

    return $count;
}

change the your-cpt-slug to match your CPT.

Ah, sorry I missed that help article. I think it works as advertised.

Here is my problem. That code changes the archive results for that custom post type on any kind of archive page, including tag archives.

My tag taxonomy is shared by this CPT (podcasts) and regular posts. So when there are posts with a tag and podcasts with the same tag, then on the tag archive page (which never has a sidebar) the podcast results are 50% and the post results are 33%.

That's why I wanted to target the number of columns to the layout, not the particular custom post type. Since the Layout Element allows me to change the sidebar on the podcast series archive pages I had hoped I could base the archive columns on that.

Ideas? The reason I'm in this situation is that I like having a sidebar on the podcast archive that advertises the other podcasts we have. However I may just need to give up on that if there isn't an easy way to do it.

Hi there,

I believe we can do a conditional filter based on the sidebar.

Example:

add_filter( 'generate_blog_get_column_count','tu_search_column_count' );
function tu_search_column_count( $count ) {
	$sidebar = generate_get_layout();
    if ( is_archive() && $sidebar == 'no-sidebar' ) { // if its an archive page w/ no sidebar 
        return 33;
    } if ( is_archive() && $sidebar != 'no-sidebar' ) { // if its an archive page that has a sidebar 
        return 50;
    }

    return $count;
}

Perfect!!!!

Thank you all so much! It seems like there is nothing your team can't do.

Nice one. Glad you got it sorted. No problem. :D

This archived topic is closed to new replies.