- This topic has 16 replies, 2 voices, and was last updated 8 years, 3 months ago by
Tom.
-
AuthorPosts
-
May 31, 2015 at 6:50 am #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
May 31, 2015 at 8:56 am #111604Tom
Lead DeveloperLead DeveloperHi 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/
May 31, 2015 at 10:18 am #111623Tara
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
May 31, 2015 at 10:36 am #111637Tom
Lead DeveloperLead DeveloperThe 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?
May 31, 2015 at 11:39 am #111648Tara
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
May 31, 2015 at 11:52 pm #111706Tom
Lead DeveloperLead DeveloperAny way to get a link to the page? Your page template looks good to me.
June 1, 2015 at 12:46 am #111714Tara
June 1, 2015 at 8:49 am #111771Tom
Lead DeveloperLead DeveloperFrom 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 ๐
June 4, 2015 at 1:15 pm #112506Tom
Lead DeveloperLead DeveloperWas 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!
June 4, 2015 at 2:29 pm #112534Tara
Sorry, I set them to expire automatically. Here’s the page template again: http://pastebin.com/n88diff0
Thanks
Tara
June 10, 2015 at 11:52 am #113800Tom
Lead DeveloperLead DeveloperYou 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 ๐
June 13, 2015 at 12:50 pm #114414Tara
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
June 14, 2015 at 12:08 am #114469Tom
Lead DeveloperLead DeveloperSimply 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;
June 14, 2015 at 7:12 am #114497Tara
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
June 14, 2015 at 9:32 am #114518Tom
Lead DeveloperLead DeveloperGood 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?
-
AuthorPosts
- You must be logged in to reply to this topic.