在显示之前编辑_Content()的值

时间:2017-05-04 作者:M Argus Chopin Gyver

我有一个任务需要在中间插入广告标签the_content() 价值

但我很困惑,因为我无法分解the_content().

有没有办法把一些代码放在the_content() 价值

或者也许有一种替代使用函数的方法while(have_post) 然后在列表上打印帖子,风格如何?

或者我可以使用某种循环(比如foreach)并在数据之间插入广告标签时逐个打印数据吗?

很抱歉,我仍然不知道如何转储the_content() 但我只能通过查看inspect元素来查看值。这是我知道的价值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 -->
你可以这样读上面的代码<!-- I NEED TO PUT THE AD-TAGS IN HERE -->

在生成的代码中间the_content(). 这可能吗?

1 个回复
SO网友:cjbj

如果您想在特定位置添加代码,可以build a shortcode 然后你可以放置[place-add] 在您的内容中您希望广告出现的确切位置。

如果不想在文本中添加短代码,可以构建filter on the_content. 在这种情况下,您需要定义一个插入代码的一般位置,例如在第二段之前。工作原理如下:

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;
  }
(我没有测试代码,因此可能需要进行一些调试)

相关推荐