0

I have a task that needs to insert ad-tags in the middle of the_content() value.

But I got confused because I can't break down the value of the_content().

Is there any way to put some code in the middle of the_content() value?

Or maybe there's an alternative to using function while(have_post) and print the post on the list kind of style?

Or can I just use kind of loop (like foreach) and print the data one by one while inserting the ad-tags between it?

I'm sorry I still don't know how to dump the value of the_content() itself, but I can only see the value by looking the inspect element. Here's the value that I know was from the_content();

<!-- NOTE: If you need to make changes to this file, copy it to your current theme's main
    directory so your changes won't be overwritten when the plugin is upgraded. -->

<!-- Post Wrap Start-->
<div class="post hentry ivycat-post">

    <!--    This outputs the post TITLE -->
    <h2 class="entry-title"><a href="http://blog.jkt48.com/team-t/2017/05/03/blog-jinan/">Blog Jinan</a></h2>

    <!--    This outputs the post EXCERPT.  To display full content including images and html, 
        replace the_excerpt(); with the_content();  below. -->
    <div class="entry-summary">
        <p>Hi~ aku Jinan dari team T~ blog SweeTie kali ini aku yang tulis<img draggable="false" class="emoji" alt="😊" src="https://s.w.org/images/core/emoji/2.2.1/svg/1f60a.svg" scale="0"> Pertama-tama aku mau ucapin terima kasih sama kalian yang selalu dukung aku, kemarin-kemarin jadwalku padat sekali karena ujian sekolah, tapi berkat kalian, sekarang aku sudah melewatinya<img draggable="false" class="emoji" alt="🙂" src="https://s.w.org/images/core/emoji/2.2.1/svg/1f642.svg" scale="0"> Aku udah lulus SMA loh, cie <img draggable="false" class="emoji" alt="😂" src="https://s.w.org/images/core/emoji/2.2.1/svg/1f602.svg" scale="0"> &nbsp;Ga kerasa banget udah 3 tahun aku belajar di […]</p>
    </div>

    <!--    This outputs the post META information -->
    <div class="entry-utility">
                    <span class="cat-links">
                <span class="entry-utility-prep entry-utility-prep-cat-links">Posted in</span> <a href="http://blog.jkt48.com/team-t/category/blog/" rel="category tag">Blog</a>            </span>
            <span class="meta-sep">|</span>
                            <span class="tag-links">
                <span class="entry-utility-prep entry-utility-prep-tag-links">Tagged</span> <a href="http://blog.jkt48.com/team-t/tag/jinan/" rel="tag">jinan</a>, <a href="http://blog.jkt48.com/team-t/tag/jkt48/" rel="tag">JKT48</a>, <a href="http://blog.jkt48.com/team-t/tag/team-t/" rel="tag">team T</a>           </span>
            <span class="meta-sep">|</span>
                <span class="comments-link"><a href="http://blog.jkt48.com/team-t/2017/05/03/blog-jinan/#comments">16 Comments</a></span>
            </div>
</div>
<!-- // Post Wrap End -->

<!-- I NEED TO PUT THE AD-TAGS IN HERE -->

<!-- NOTE: If you need to make changes to this file, copy it to your current theme's main
    directory so your changes won't be overwritten when the plugin is upgraded. -->

<!-- Post Wrap Start-->
<div class="post hentry ivycat-post">

    <!--    This outputs the post TITLE -->
    <h2 class="entry-title"><a href="http://blog.jkt48.com/team-t/2017/05/02/ngebolang-bareng-fia/">Ngebolang Bareng Fia</a></h2>

    <!--    This outputs the post EXCERPT.  To display full content including images and html, 
        replace the_excerpt(); with the_content();  below. -->
    <div class="entry-summary">
        <p>Bismillah, Halo semua! Ini tulisan blog aku yang kedua hehe baru dikit ya. &nbsp; Minggu terakhir bulan april alhamdulillah team T dapet banyak kegiatan on air dan off air! Dari hari kamis team T seperti biasa ada show RKJ di theater, lanjut hari Jum’at team T ada di acara Mama Amy (kedua kalinya Team T […]</p>
    </div>

    <!--    This outputs the post META information -->
    <div class="entry-utility">
                    <span class="cat-links">
                <span class="entry-utility-prep entry-utility-prep-cat-links">Posted in</span> <a href="http://blog.jkt48.com/team-t/category/blog/" rel="category tag">Blog</a>            </span>
            <span class="meta-sep">|</span>
                            <span class="tag-links">
                <span class="entry-utility-prep entry-utility-prep-tag-links">Tagged</span> <a href="http://blog.jkt48.com/team-t/tag/fia/" rel="tag">fia</a>, <a href="http://blog.jkt48.com/team-t/tag/fidly-immanda-zahra/" rel="tag">fidly immanda zahra</a>, <a href="http://blog.jkt48.com/team-t/tag/jkt48/" rel="tag">JKT48</a>, <a href="http://blog.jkt48.com/team-t/tag/melati/" rel="tag">melati</a>, <a href="http://blog.jkt48.com/team-t/tag/team-t/" rel="tag">team T</a>           </span>
            <span class="meta-sep">|</span>
                <span class="comments-link"><a href="http://blog.jkt48.com/team-t/2017/05/02/ngebolang-bareng-fia/#comments">9 Comments</a></span>
            </div>
</div>
<!-- // Post Wrap End -->

and you can read above that code like this <!-- I NEED TO PUT THE AD-TAGS IN HERE -->

In the middle of code that produces by the_content(). Is this possible?

  • Are you going to put <!-- I NEED TO PUT THE AD-TAGS IN HERE --> in content? If, yes there is a for-sure way then. – BlueSuiter May 4 '17 at 12:16
  • yes. I want to put the code at that specific place. Between the first post end and second post start. but that's just an example. In the practice, I want to put the <!-- I NEED TO PUT THE AD-TAGS IN HERE --> after the third post end, and so on @AmitChauhan – M Argus Chopin Gyver May 5 '17 at 4:08
3

If you want a specific place to add your codes, you could build a shortcode and then you could place something like [place-add] in your content in the exact place where you want the ad to appear.

If you don't want to add a shortcode to your text, you can build a filter on the_content. In that case you need to define a general place to instert the codes, for instance right before the second paragraph. That would work like this:

add_filter ('the_content','wpse265825_add_ad');
function wpse265825_add_ad($content) {
  $ad = 'this is my ad code';
  // find the second paragraph
  $pos = strpos($content, '<p>', strpos($content, '<p>') + 1);
  // insert code in that position
  $content = substr_replace($content, $ad, $pos, 0);
  return $content;
  }

(I didn't test the code, so some debugging may be necessary)

  • I'm really new on wordpress. can I put the function on the themes file ? or I need to put on the other file ? – M Argus Chopin Gyver May 5 '17 at 4:25
  • You can add functions in fucntions.php or can include any other file in which you can keep all of your custom functions. – BlueSuiter May 5 '17 at 5:31
  • am I need to call the function ? or the add_filter ('the_content','wpse265825_add_ad'); is already called the function ? because I can't see any changes happen with the_content() value.. – M Argus Chopin Gyver May 5 '17 at 6:52
  • You put the filter and the corresponding function in your child theme's functions.php. You can echo something in the function to see if the filter gets called at all, then tweak the function to get the ad where you want it. – cjbj May 5 '17 at 7:55
  • ah the code works ! but it's put the this is my ad code on above the first <!-- Post Wrap Start-->. and a question, are these filter can do repetation ? like if I have 6 posts, I will show the this is my ad code after the third post and so on ?? – M Argus Chopin Gyver May 5 '17 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.