[Resolved] Blog Layout depending on category

Home Forums Support Blog Layout depending on category

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #69396
    Vsevolod Serbin

    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?

    #69409
    Tom
    Lead Developer
    Lead Developer

    Hmm let me think about the best way to do this – will get back to you on this one in the morning.

    Thanks!

    #69537
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #69743
    Vsevolod Serbin

    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.

    #69798
    Tom
    Lead Developer
    Lead Developer

    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;
    #72369
    Vsevolod Serbin

    Tom, thank you for help.

    #72435
    Tom
    Lead Developer
    Lead Developer
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.