[Resolved] Element after first paragraph

Home Forums Support [Resolved] Element after first paragraph

Home Forums Support Element after first paragraph

Viewing 13 posts - 1 through 13 (of 13 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. πŸ™‚

    #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. πŸ˜€

    #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 πŸ˜€

    #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. πŸ˜€

    #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

    #2517656
    Andrew

    Sorry, I mean to post this reply on another thread. Is there a way to delete this reply?
    β€”β€”β€”β€”β€”β€”β€”-

    I just tried that and got the error “Your PHP code changes were rolled back due to an error on line 122 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.

    syntax error, unexpected end of file”

    This is the code I’m using, including the first part which is working fine.

    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, 10, $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 );
    }
    add_filter( ‘the_content’, ‘insert_featured_image2’, 20 );
    function insert_featured_image2( $content ) {
    global $post;
    $inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_fiftieth_paragraph”]’);
    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $inserted_hook, 50, $content );
    }

    #2517761
    Fernando
    Customer Support

    Hi Andrew,

    We’ll reply in the topic you created.

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