[Resolved] Element after first paragraph

Home Forums Support [Resolved] Element after first paragraph

Home Forums Support Element after first paragraph

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1937342
    Cynthia

    Hello. I wanted my affiliate notice to show up in every post so I added a hook element that currently is set to generate after entry title. I want the notice of course to be before any of my affiliates are used but I don’t love that it is the first thing people read. Is there a way to set it so that it will just come after my first paragraph in each post? I looked through the choices but I’m not very savvy so I’m probably just missing it. Please help πŸ™‚ Thanks
    Cynthia

    #1937353
    Elvin
    Staff
    Customer Support

    Hi Cynthia,

    We can try making a hook after the first paragraph if you want.

    Add this PHP snippet:

    add_shortcode('portable_hook', function($atts){
    	ob_start();
            $atts = shortcode_atts( array(
                'hook_name' => 'no foo'
            ), $atts, 'portable_hook' );
    		do_action($atts['hook_name']);
    	return ob_get_clean();
    });
    
    add_filter( 'the_content', 'insert_featured_image', 20 );
    function insert_featured_image( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 1, $content );
        }
        return $content;
    }
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
      $closing_p = '</p>';
      $paragraphs = explode( $closing_p, $content );
      foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
         $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
         $paragraphs[$index] .= $insertion;
        }
      }
      return implode( '', $paragraphs );
    }

    After activating this PHP snippet on your site, you can hook things on the first paragraph of the content using after_first_paragraph hook. πŸ™‚

    A wise man once said:
    "Have you cleared your cache?"

    #1939694
    Cynthia

    Hey Elvin,
    Thanks for the help. I’m not very tech savvy. Would you suggest I use the plugin code snippets to add the PHP code you suggested or should I set up a child theme? Those were the two suggestions I saw when I was searching how to add PHP code in generate press. Thanks in advance πŸ™‚
    Cynthia

    #1939785
    Elvin
    Staff
    Customer Support

    Any of the two choices should be fine but for your case, I’d suggest Code Snippets plugin as it’s easier to setup. πŸ˜€

    A wise man once said:
    "Have you cleared your cache?"

    #1975299
    Cynthia

    Hey there. I finally got around to trying this and I’m screwing up somewhere because it’s not working for me. So I went with your advice and added the snippet plug-in. I then did “add new” as a css and added the code you gave me above (I’m assuming that is what I should have added it as). Then I am given the choice of run snippet everywhere (which I chose), only run in admin area, only run on site front end, only run once. Then I went in under “elements” and tried to hook to the first paragraph. However I’m not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!

    #1975320
    Elvin
    Staff
    Customer Support

    I then did β€œadd new” as a css and added the code you gave me above (I’m assuming that is what I should have added it as).

    The code provided is a PHP snippet. It should be added as PHP. πŸ™‚

    However I’m not seeing after_first_paragraph option. Could you help me where I am going wrong. Thank you!

    It won’t show up on the display rule because it’s a custom hook we’ve manually added.

    On the “Hook” dropdown of the element, you should select “Custom”. A field will appear. That’s were you type in after_first_paragraph πŸ˜€

    A wise man once said:
    "Have you cleared your cache?"

    #1976965
    Cynthia

    Yay it worked! Thanks Elvin so much for your patience and kindness!

    #1976966
    Cynthia

    πŸ™‚

    #1977142
    Elvin
    Staff
    Customer Support

    Nice one! Glad you got it to work. Thanks for letting us know. No problem. πŸ˜€

    A wise man once said:
    "Have you cleared your cache?"

    #2160434
    Mohammed

    Hi there,

    thanks for that.

    I’m having an issue when adding this.

    You can see the element The Why trust us box after the first paragraph. There’s a table of content that is being nested with the block element. The table of content should be shown after that box.

    https://stroller-envy.com/uppababy-vista/

    #2160835
    David
    Staff
    Customer Support

    I will reply to your topic

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