Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...