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

Home Forums Support [Resolved] PHP function by GP Hooks doesn't work, but by including to functions.php it work

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

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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.

    #377484
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You should add it using one of these methods: https://docs.generatepress.com/article/adding-php/

    #377733
    Marek

    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.

    #377800
    Marek

    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…

    #377808
    Marek

    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

    #377811
    Marek

    Snippets works too with correct quotation mark.

    #377834
    Marek

    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

    #377878
    Tom
    Lead Developer
    Lead Developer

    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.

    #378329
    Marek

    Thanks a lot!

    #378378
    Tom
    Lead Developer
    Lead Developer

    No problem 🙂

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