Site logo

Archived topic

PHP function by GP Hooks doesn't work, but by including to functions.php it work

9 replies · Started by Marek on September 1, 2017

Viewing posts 1–10 of 10

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.

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.

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...

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

Snippets works too with correct quotation mark.

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

This 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.

Thanks a lot!

No problem :)

This archived topic is closed to new replies.