Archived topic
Blog Layout depending on category
6 replies · Started by Vsevolod Serbin on January 27, 2015
Hello!
At first I would like to thank you for such excellent job. Generatepress is one of the best WP Themes that I had worked with.
I have a question. I use masonry blog layout, but in some categories I would like to use standart layout, with full-width blocks.
Is it possible to do without creating special templates as category-"slug".php?
Hmm let me think about the best way to do this - will get back to you on this one in the morning.
Thanks!
Alright, it's not overly easy, but it's do-able.
First we need to add some custom PHP - this plugin is good for adding it: https://wordpress.org/plugins/code-snippets/
function generate_dequeue_scripts() {
// If the category ID is 79, dequeue masonry
if ( is_category( '79' ) ) :
wp_dequeue_script( 'blog-scripts' );
wp_dequeue_script( 'jquery-masonry' );
wp_dequeue_script( 'blog-imagesloaded' );
wp_dequeue_style( 'blog-styles' );
endif;
}
add_action( 'wp_print_scripts', 'generate_dequeue_scripts', 100 );
add_action( 'wp_enqueue_scripts', 'generate_dequeue_scripts', 100 );
You'll need to replace "79" with the ID of your category.
To do multiple categories, you can do this: if ( is_category( '79' ) || is_category( '80' ) || is_category( '81' ) ) :
Now you'll need to add a little bit of custom CSS:
.category-79 .page-header,
.category-79 .inside-article {
margin-right: 0;
}
Again, you'll have to change the ID.
Hope this helps :)
That works!
Tom, thanks a lot!
Is it posible to use the same code for custom post types? I'm totally incompetent in PHP but I see the conditional tags.
I changed "is_category" to "is_tax" but that broke my site for a while :)
I really appreciate your help and if this question is too much, I'll understand.
You can target custom post types like this:
$post_type = get_post_type();
if ( 'books' == $post_type ) :
// Stuff in here will only happen to the "books" custom post type
endif;
Tom, thank you for help.
You're welcome :)