Wordpress PHP - need to retain nextpage paginating tag in custom query

风格不统一 提交于 2019-12-25 00:24:07

问题


Preface: i am not a good coder.

I need to use $post->post_content to get the raw post so that i can use the EXPLODE php command. But when i do use $post->post_content, it filters out the tags that are in my post which need to be retained. here is my script. what am i doing wrong? thanks!

<?php

$content = apply_filters('the_content', $post->post_content);

$contents = array_filter(explode("</p>", $content));

foreach ($contents as $content) {
    if (strpos($content, '<img') !== false ) {
        echo $content;
        echo "</p>after image ad";
    } else {
        echo $content;
        echo "</p>";
    }
}

?>

I'm basically trying to insert an Ad after any paragraph that only contains an image.


回答1:


It seems that when you call:

$content = apply_filters('the_content', $post->post_content);

It applies autop to split the paragraphs, and also applies do_shortcode on all shortcodes.

So you'd better not to call apply_filters here, but call wpautop instead:

See: http://codex.wordpress.org/Function_Reference/wpautop

<?php

$content = wpautop( $post->post_content );

$contents = array_filter(explode("</p>", $content));

$result = '';

foreach ($contents as $content) {
    $result .= $content.'</p>';
    if (strpos($content, '<img') !== false ) {
        $result .= "after image ad";
    }
}

$content = apply_filters('the_content', $result);

echo $result;

?>


来源:https://stackoverflow.com/questions/28221837/wordpress-php-need-to-retain-nextpage-paginating-tag-in-custom-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!