Support » Theme: GeneratePress » How to disable a hook on a specific page?

  • Resolved themedleb

    (@themedleb)


    I need to put a shortcode (of related posts) in the hook “After Content”, it appears successfully in posts but It appears also in every custom page, It’s not needed for pages to have related posts, can you please show me a way to disable it from only some specific pages like “Home” page and “About” page and “Contact us” page …?

    Thank you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Leo

    (@leohsiang)

    Hi there,

    Try something like this:

    add_action( 'generate_after_content','lh_add_related_post' );
    function lh_add_related_post() { 
      if ( !is_page( array( 'Home', 'About' ) )  ){?>
        shoercode here
      <?php }
    }

    Thank you Leo, can you make it clear please?

    Can I write posts/pages ID instead of Home, About?

    And why should I write shortcode there?

    I already put my shortcode in the hooks (after content hook), I just need to deactivate it in some specific pages, I don’t think I need to put shortcode to disable a hook on a specific page.

    Theme Author Tom

    (@edge22)

    You can replace the names that Leo put with IDs:

    add_action( 'generate_after_content', 'lh_add_related_post' );
    function lh_add_related_post() { 
      if ( ! is_page( array( 10, 15 ) ) ){ 
      ?>
        shortcode here
      <?php }
    }

    This code will actually place your shortcode into that hook.

    If you’re using another method like GP Hooks, you should be posting in our premium forum: https://generatepress.com/support

    Let us know if you need more info 🙂

    Thank you Tom,

    So this code will place the shortcode in the after_content hook of pages 10 and 15?

    Leo

    (@leohsiang)

    That will exclude pages 10 and 15. That’s the use of exclamation mark.

    Thank you all so much, it worked, but it only shows the shortcode’s code, instead of the functionality of the shortcode.

    Theme Author Tom

    (@edge22)

    Ah, you’ll need to do this:

    add_action( 'generate_after_content', 'lh_add_related_post' );
    function lh_add_related_post() { 
        if ( ! is_page( array( 10, 15 ) ) ) {
            echo do_shortcode( '[your_shortcode_here]' ); 
        }
    }

    As always, be careful with PHP – it can cause errors if the syntax is wrong.

    Thank you Tom and Leo, it’s perfect now.

    You won a premium subscriber in the next few days.

    Hope you integrate the option to disable/deactivate specific hooks on each page/post options.

    Thank you, and good luck.

    Theme Author Tom

    (@edge22)

    Thank you! Glad we could help 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to disable a hook on a specific page?’ is closed to new replies.