Join the Soledad Facebook Users Group here
for Share, assistance, discussion, and Updates related to Soledad WordPress Theme.

If you can't create a new ticket - you can send us an email via our contact form and we will get back to you.

Okay
  Public Ticket #2793597
How to add html snippet in the end of each post?
Closed

Comments

  • LazyBit started the conversation

    Hi dev,

    I would like to automatically add to ALL past and future posts an html snippet in the end. I mean something like:

    "Thank you for reading so far, please share and scroll down to see if there are more article you can be interested in, please donate" etc.

    What's the best way to do that?

    PS I setup this ticket as public because maybe can be interesting for someone else too.

  •  2,492
    PenciDesign replied

    Hi,

    Please follow the instruction:

    1. Install the Code Snippet plugin,

    2. Create a new snippet and paste the following code:

    function lazybit_thankyou_content($content) {
        $fullcontent = $content;
        if ( is_single() ) {
            $after_content = '<div class="user_thank">Thank you for reading so far, please share and scroll down to see if there are more article you can be interested in, please donate</div>';
            $fullcontent = $content.$after_content;
        }
        return $fullcontent;
    }
    add_filter('the_content', 'lazybit_thankyou_content');
    Done. This code will add your content after the main post content in a single post.

    Best Regards,
    PenciDesign.

  • LazyBit replied

    It works great, thank you!

  • LazyBit replied

    ... But I can't make it working with a more complex content. Example:

    ------------

    Thank you for reading so far,  now you have 2 options to donate:

    1. Option 1: PayPal
    2. Option 2: Bitcoin

    See you!

    ------------

    I don't know how to insert above html content in your code, everytime I try it breaks the snippet.

    So far the best way I found is to convert the above html content to a shortcode using Shortcoder plugin and then insert the shortcode in the code you provided me this way:

    function lazybit_thankyou_content($content) {
        $fullcontent = $content;
        if ( is_single() ) {
            $after_content = '[sc name="after-post"]';
            $fullcontent = $content.$after_content;
        }
        return $fullcontent;
    }
    add_filter('the_content', 'lazybit_thankyou_content');

    It works, but I would like to not using any external plugin. Any help?

  •  2,492
    PenciDesign replied

    Hi,

    You MUST write your content in HTML format, for example:

    function lazybit_thankyou_content($content) {
        $fullcontent = $content;
        if ( is_single() ) {
            $after_content = '<p>Thank you for reading so far,  now you have 2 options to donate:</p>
    <ol><li>Option 1: <a href="#" target="_blank" rel="nofollow noreferrer noopener">PayPal</a></li><li>Option 2: <a href="#" target="_blank" rel="nofollow noreferrer noopener">Bitcoin</a></li></ol>
    <p>See you!</p>';
            $fullcontent = $content.$after_content;
        }
        return $fullcontent;
    }
    add_filter('the_content', 'lazybit_thankyou_content');
    You can go to https://html-online.com/editor/ and replace your content here.

    Best Regards,
    PenciDesign.

  • LazyBit replied

    Great, now I can do it this way :)

    Thank you for your patience, you rock!

  • LazyBit replied

    Your snippet is working great. Now I need that after-content to not being generated on one or more specific posts. Is there a way to add a kind of exclusion-list to your snippet?

  •  2,492
    PenciDesign replied

    Hi,

    Please modify the code like this:

    function lazybit_thankyou_content($content) {
        global $post;
        $fullcontent = $content;     $exclude_post_id = array(1681,1673);
        if ( is_single() && ! in_array( $post->ID, $exclude_post_id ) ) {
            $after_content = 'your content';
            $fullcontent = $content.$after_content;
        }
        return $fullcontent;
    }
    add_filter('the_content', 'lazybit_thankyou_content');

    Replace 1681,1673 with the post ID list you want to exclude, separate by a comma.

    If you don't know how to find the post ID, please read the document here.

    Regards,
    PenciDesign.

  • LazyBit replied

    It works perfect, thank you.