Enable masonry on other post types

Home Forums Support Enable masonry on other post types

Home Forums Support Enable masonry on other post types

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #111573
    Tara

    Hi Tom

    I want to use the masonry layout on some custom page templates and custom post types. To do that, I’d need to change the conditional tags in four functions (line 702, 911, 943 and 993 in generate-blog.php):

    if ( is_home() || is_archive() || is_search() || is_attachment() || is_tax() ) :

    and line 80:

    if ( 'post' !== get_post_type() )
    	$masonry = 'false';

    Could you make it easier to change which content types use masonry, by setting them in one place that we can override?

    Thanks

    Tara

    #111604
    Tom
    Lead Developer
    Lead Developer

    Hi Tara,

    You can actually use a filter to do this – no need to change core files.

    For example:

    add_filter('generate_blog_masonry','generate_blog_enable_post_type_masonry');
    function generate_blog_enable_post_type_masonry()
    {
            $type = get_post_type();
    
            // If we're on the "books" post type, enable masonry
    	if ( 'books' == $type )
    		return 'true';
    
    	// Check page template by file name
    	if ( is_page_template('my-custom-template.php') )
    		return 'true';
    	
            // Otherwise, disable it
    	return 'false';
    }

    Adding PHP: http://generatepress.com/knowledgebase/adding-php-functions/

    #111623
    Tara

    Hi Tom

    Thanks, that solves the post types in line 80, but it’s only halfway there. On my custom page templates it only adds the two Masonry scripts, without all the necessary classes and CSS to actually display the masonry layout.

    There are two things I’m trying to achieve:

    – My top-level parent pages will show their child pages as excerpts (using custom template child-pages.php)
    – My static front page will show a mixture of featured posts/pages/events as excerpts (using custom template front-page.php)

    As these are pages, they’re excluded by the conditional tags in line 702, 911, 943 and 993. I got the masonry working temporarily on my local copy by adding is_page_template(‘child-pages.php’) to those lines, but I’m looking for a proper solution without editing the core files.

    Hope that makes sense.

    Thanks

    Tara

    #111637
    Tom
    Lead Developer
    Lead Developer

    The filter above should bypass any exclusions in the core code – shouldn’t matter if it’s a page or post.

    Does your page template have all of the hooks that the core GP pages have? Masonry uses quite a few of those hooks to insert HTML and other things, so it’s important that they’re still present.

    Can you possible show me a pastebin (http://pastebin.com/) of your page template?

    Do you have a link to your page template where masonry isn’t working?

    #111648
    Tara

    Ah, I see – maybe I’ve removed or changed something I shouldn’t have. It’s still work in progress, but here’s my current child-pages.php template: http://pastebin.com/wUGFJStS

    It lists the child pages in full-width separate containers, without the masonry grid.

    Thanks

    Tara

    #111706
    Tom
    Lead Developer
    Lead Developer

    Any way to get a link to the page? Your page template looks good to me.

    #111714
    Tara

    Here’s the current HTML output of that template: http://pastebin.com/vpzw4tWF

    Thanks

    Tara

    #111771
    Tom
    Lead Developer
    Lead Developer

    From the looks of that it’s not working at all.

    I’ll have to set some time aside to create my own page template to do some testing – I’ll let you know what I figure out ๐Ÿ™‚

    #112506
    Tom
    Lead Developer
    Lead Developer

    Was just getting to this now, but it seems you removed your pastebin entries?

    Can you possibly re-post your page template code so I can replicate it exactly?

    Thanks!

    #112534
    Tara

    Sorry, I set them to expire automatically. Here’s the page template again: http://pastebin.com/n88diff0

    Thanks

    Tara

    #113800
    Tom
    Lead Developer
    Lead Developer

    You were right about changing those conditionals for is_home() etc..

    What I’ve done is create a function which looks for those conditionals instead of listing them out multiple times.

    Now, in the next version of Generate Blog/GP Premium, you’ll be able to do this:

    add_filter('generate_blog_masonry','generate_blog_enable_post_type_masonry');
    function generate_blog_enable_post_type_masonry()
    {
    	// Check page template by file name
    	if ( is_page_template('page-testing.php') )
    		return 'true';
    	
            // Otherwise, disable it
    	return 'false';
    }
    
    add_filter('generate_blog_activate_masonry','generate_blog_activate_page_template_masonry');
    function generate_blog_activate_page_template_masonry()
    {
    	// Check page template by file name
    	if ( is_page_template('page-testing.php') )
    		return 'true';
    	
            // Otherwise, disable it
    	return 'false';
    }

    page-test.php being the file name of your page template.

    If you’d like to test it now instead of waiting for the update, shoot me an email at support@generatepress.com and I’ll give you the file to replace ๐Ÿ™‚

    #114414
    Tara

    Thanks Tom, that will make it much easier.

    A related problem is that the post excerpts on my custom pages weren’t showing their featured images, because the generate_blog_post_image function excludes singular pages. I’ve made a new function to change that, but I can’t work out how to remove the original action, which is still loading featured images on the normal blog. What’s the right way to override a plugin function in a child theme?

    Thanks

    Tara

    #114469
    Tom
    Lead Developer
    Lead Developer

    Simply add your function to your child name, but make sure it has the same function name, and be sure to include a function_exists check.

    For example:

    if ( ! function_exists( 'generate_blog_post_image' ) ) :
    function generate_blog_post_image()
    {
    	// Your custom stuff in here
    }
    endif;
    #114497
    Tara

    Doesn’t that have no effect because the function does already exist? The Blog add-on is a plugin, which is loaded before the theme, so the function has already been declared.

    (So at first when I’d used the same function name without a function_exists check, I got an error: Cannot redeclare generate_blog_post_image() (previously declared in … generate-blog.php:91))

    One solution I’ve found is to put my version of the function (with the same name) in a custom plugin so that it’s loaded before the Blog plugin. I’ve used mu-plugins which is always loaded first, though a standard plugin starting with a letter before G should also load before the GeneratePress plugin.

    The other option in this case is just to keep the core function for the normal blog pages, and in the child theme add an extra function (with a different name) that only applies to my custom blog pages.

    Either way, I wanted to learn how to override plugin functions, in case there’s anything else I need to customise that doesn’t have a filter.

    Thanks

    Tara

    #114518
    Tom
    Lead Developer
    Lead Developer

    Good point, I’m so used to overriding theme functions, didn’t think about it being inside a plugin.

    I would just create a new plugin for your custom functions using a plugin like this: https://wordpress.org/plugins/pluginception/

    Another option would be to unhook the original function, and then hook yours in. Is your post image set to display above the title or below?

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