[Resolved] Adding a function in functions.php

Home Forums Support Adding a function in functions.php

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1361594
    sjoerd89

    Hi everyone,

    i am trying to get a function to work on my website. At the moment i use it in functions.php of my child theme. The code i use is

    function wpb_linkedin_share_after($content) { 
    $sharecode .= '<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>
    <script type="IN/Share" data-counter="top"></script>'; 
    $newcontent = $content . $sharecode; 
    return $newcontent; 
    } 
    add_filter('the_content', 'wpb_linkedin_share_after');

    This function makes a linkedin share button but currently shows up on every page of my website.
    I would like to only show this on post pages.

    Is it better to use this in GP-hooks? because there i also do not see the option for post pages.
    Thanks for reading 🙂

    Sjoerd

    #1361680
    David
    Staff
    Customer Support

    Hi there,

    try this:

    function wpb_linkedin_share_after($content) { 
      if ( is_single() ) {
        $sharecode .= '<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>
        <script type="IN/Share" data-counter="top"></script>'; 
        $content .= $sharecode; 
      }
      
      return $content; 
    } 
    add_filter('the_content', 'wpb_linkedin_share_after');

    If you wanted to add it after the content you could do it with a Hook element.
    You only need this for the hook content:

    <script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>
    <script type="IN/Share" data-counter="top"></script>

    And you would select the after_content hook and use the Posts > All Posts display rule.

    #1361692
    sjoerd89

    And you would select the after_content hook and use the Posts > All Posts display rule.

    This part is a bit confusing to me sorry.

    I only see execute PHP and disable Hook as options

    #1361706
    David
    Staff
    Customer Support

    You should see a tab called Display Rules:

    https://docs.generatepress.com/article/hooks-element-overview/#display-rules

    In Location, you can select Posts, then a second field aside that will display which you leave set to All Posts.

    #1361756
    sjoerd89

    -edit- found the other hooks under legacy hooks now, thanks for your help!!

    I had elements turned off on my website so i never saw this option.
    The code that i had in Hooks is now gone tho?
    I do not see the hooks part anywhere anymore now.

    I got the other part working now 🙂

    #1361762
    David
    Staff
    Customer Support

    In Appearance > Elements – at the top of the screen you will see Legacy Hooks. You should find you your old codes in there.

    #1361767
    sjoerd89

    In Appearance > Elements – at the top of the screen you will see Legacy Hooks. You should find you your old codes in there.

    thanks a lot for your help!

    #1361795
    David
    Staff
    Customer Support
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.