Peter Elias
Peter Elias

Reputation: 317

insert default text in the middle of posts

I have a wordpress includes around 1000 post, I want to add a default text (HTML formatted) in the middle of each post after loading. so in the future may I can change this text at once and show another text instead if I want.

I found functions to add text after & before the posts, but nothing is used to add inside the posts.

I think that I can add the text after 1st </p>

Is there any solution for this?

Upvotes: 0

Views: 1948

Answers (4)

cheinan
cheinan

Reputation: 79

The simplest way is: 1 - Add your HTML code after the post content in a "display: none;" container 2 - Under that container add small javascript:

var containerID = document.getElementById("container");
    var middle-Post-Elements = document.getElementsByClassName("middle-Element-class");
    var i;
    for (i = 0; i < middle-Post-Elements.length; i++) {
      middle-Post-Elements[i].innerHTML = containerID.innerHTML;
    } 

3 - Now in your post editor just add this HTML tag were ever you want the code to appear:

<div class="middle-Element-class"></div>

Upvotes: 0

Peter Elias
Peter Elias

Reputation: 317

I found below code and added to function.php and it works good

<?php

//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

    $ad_code = '<div>Ads code goes here</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen

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 );
}
?>

Upvotes: 1

Kamran
Kamran

Reputation: 2741

I think WordPress shortcodes can help you with this. In every post you have to mention short code like this. [Myshortcode] and than you can define the functionality in your function.php.

Simple example

//[foobar]
function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

This will create [foobar] shortcode that returns as: foo and bar

here is the link for detail description

For jquery based solution to put content after first p you can do something like this.

$("#mydiv p:first-child").after("<div>Here is my html</div>");

Fiddle is HERE

Upvotes: 1

taketheleap
taketheleap

Reputation: 97

Is there a common div or marker that you can use to add text after?

Example:

<div id="first">Lorem ipsum</div>
<!-- where you want text -->
<div id="second">Lorem ipsum</div>

Perhaps a jQuery script, added to single.php can work?

<script>
$( "#first" ).after( "<p>My inserted text.</p>" );
</script>

Upvotes: 1

Related Questions