[Resolved] adding widget area to 3rd paragraph in the single page

Home Forums Support [Resolved] adding widget area to 3rd paragraph in the single page

Home Forums Support adding widget area to 3rd paragraph in the single page

Viewing 15 posts - 31 through 45 (of 52 total)
  • Author
    Posts
  • #2428638
    David
    Staff
    Customer Support

    Can you try using this snippet instead ?

    
    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_element', 20 );
    function insert_element( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_third_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 3, $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 );
    }
    #2428802
    Patricia

    Hello David!

    Ok it works πŸ™‚ but when I try to copy and paste the code to create the next custom hook I have this erros message: https://ibb.co/VVfd7Hm
    thank you!

    #2428930
    David
    Staff
    Customer Support

    Ok, you can’t duplicate the code.
    If you put it back to the one code where it was working ( and double check that πŸ™‚ )

    Now try adding this: – i set it to the 7 seventh paragraph. That will return a hook titled: after_seventh_paragraph

    
    add_filter( 'the_content', 'insert_element_seventh', 25 );
    function insert_element_seventh( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
        }
        return $content;
    }

    If that works then add another code for the after_eleventh_paragraph

    
    add_filter( 'the_content', 'insert_element_eleventh', 30 );
    function insert_element_eleventh( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_eleventh_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 11, $content );
        }
        return $content;
    }
    #2428939
    Patricia

    Hi david, yes the code was working, the only problem is that couldnt duplicate. I will test the new codes and will confirm with you! thank you so much! πŸ™‚ and talk soon!

    #2428946
    David
    Staff
    Customer Support

    Awesome – let us know πŸ™‚

    #2431641
    Patricia

    Hi David! I am trying the additional codes,

    Here is the situation:

    1. I created a hook with testimonials with the intention to place it under the 7th paragraph
    2. I created another hook with a table to be placed under the 11th paragraph

    however, all placements are showing(repeating) the related post hook. and they appear one after another instead of under paragraphs 7 and 11. you can see it on the link I am sharing! thank you!

    #2432667
    David
    Staff
    Customer Support

    Hmm…. peculiar and we’re to begin with debugging that.
    First thing i noticed, in the Query Loop block, the Headline Block with the Date Meta is set to a p – could you change that to DIV ? This will avoid it getting counted in the content.

    In the meantime ill try and get my head around what is happening here πŸ™‚

    #2435531
    Patricia

    Hi David!

    Where do you mean the query look block? This is the first block in which I added the 3 suggested articles, correct? If this is the case, I deleted it and added an info box for testing purposes, and the problem persists. the link is in private area!

    thank you!

    #2439006
    David
    Staff
    Customer Support

    Sorry for the delay, i just booked myself in to “how not to be an idiot and not check the function names” class lol

    So the issue is with the 2 additional snippets i provided here:

    https://generatepress.com/forums/topic/adding-widget-area-to-3rd-paragraph-in-the-single-page/page/3/#post-2428930

    Both i have now fixed, can you give them a try ?

    #2439234
    Patricia

    Sure will give it a try! No worries! You are doing excellent work, David!! πŸ™‚ will try and let you know! Thanks

    talk soon!

    #2439290
    Patricia

    Hi David, now, it only displays thee box after the 3rd paragraph.

    maybe I am not pasting it correctly? I tried like this

    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_element_seventh’, 25 );
    function insert_element_seventh( $content ) {
    global $post;
    $inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_seventh_paragraph”]’);
    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
    }
    return $content;
    }

    and have also tried just adding this code alone as well

    add_filter( ‘the_content’, ‘insert_element_seventh’, 25 );
    function insert_element_seventh( $content ) {
    global $post;
    $inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_seventh_paragraph”]’);
    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
    }
    return $content;
    }

    in any case it shows only after the 3rd paragraph!

    thank you so so much!

    #2439298
    David
    Staff
    Customer Support

    OK, remove any snippets you have so far and add:

    
    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_element', 20 );
    function insert_element( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_third_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 3, $content );
        }
        return $content;
    }
    
    add_filter( 'the_content', 'insert_element_seventh', 25 );
    function insert_element_seventh( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
        }
        return $content;
    }
    
    add_filter( 'the_content', 'insert_element_eleventh', 30 );
    function insert_element_eleventh( $content ) {
        global $post;
        $inserted_hook = do_shortcode('[portable_hook hook_name="after_eleventh_paragraph"]');
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $inserted_hook, 11, $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 );
    }

    Then there should be three hooks for you to use:

    after_third_paragraph
    after_seventh_paragraph
    after_eleventh_paragraph

    #2439352
    Patricia

    Oh No! It is not working :/ not even like this. shows only after_third_paragraph

    #2439471
    David
    Staff
    Customer Support

    Do you have any cache plugins ? If so can you try clearing those ?

    #2439510
    Patricia

    yes cleared caching! πŸ™‚

Viewing 15 posts - 31 through 45 (of 52 total)
  • You must be logged in to reply to this topic.