Home › Forums › Support › PHP function by GP Hooks doesn't work, but by including to functions.php it work
- This topic has 9 replies, 2 voices, and was last updated 6 years, 3 months ago by
Tom.
-
AuthorPosts
-
September 1, 2017 at 3:26 am #377258
Marek
Hello,
I try to use the following function (which shows only category 1 blogs on front page) by GP Hooks but it works only if I include it to your functions.php file, which I don’t want to do, because I expect, that first update will delete my work.
————- START CODE ————
<?php
function my_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( ‘cat’, ‘1’);
}
}
add_action( ‘pre_get_posts’, ‘my_home_category’ );
?>
————- END CODE ————
What em I doing wrong? (It is included exactly as described higher between Start and End Code)Thanks a lot.
September 1, 2017 at 10:46 am #377484Tom
Lead DeveloperLead DeveloperHi there,
You should add it using one of these methods: https://docs.generatepress.com/article/adding-php/
September 2, 2017 at 12:54 am #377733Marek
Hello,
Thanks a lot for fast answer! I will try and give you know about my progress.
Can you tell me please (or give a link) which PHP code is possible to include by hooks and which is not possible? Because there are some functions that works, other ones doesn’t? (I was trying to find the difference but didn’t succeed.)
Eg. https://docs.generatepress.com/article/using-columns-in-the-blog/
or https://docs.generatepress.com/article/using-hooks/ works.My example doesn’t.
Thanks for GP, it’s great human interface.
September 2, 2017 at 5:19 am #377800Marek
Hello,
I’ve tried Snippets and creating plugin by Pluginception. Both fail. It works only if the function is included to functions.php file.If any idea, thanks a lot…
September 2, 2017 at 5:41 am #377808Marek
Hello again.
I maybe found the solution. When I use this quotation mark “‘” it works from plugin, maybe from Snippets too. If I use this “‘” it doesn’t work.Thanks a lot for you help.
Marek
September 2, 2017 at 5:56 am #377811Marek
Snippets works too with correct quotation mark.
September 2, 2017 at 6:56 am #377834Marek
Hi, there,
last question… Can you tell me please (or give a link) which PHP code is possible to include by hooks and which is not possible? Because there are some functions that works, other ones doesn’t?Thanks
September 2, 2017 at 8:32 am #377878Tom
Lead DeveloperLead DeveloperThis code should work anywhere:
function my_home_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '1'); } } add_action( 'pre_get_posts', 'my_home_category' );
Only things that output physical elements that you can see should go in GP Hooks. So if you’re adding an advertisement, or a box of some sort, it would go in GP Hooks.
September 3, 2017 at 8:38 am #378329Marek
Thanks a lot!
September 3, 2017 at 9:47 am #378378Tom
Lead DeveloperLead DeveloperNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.