php if语句和用于在帖子内放置内容的段落代码

时间:2014-01-09 08:22:59

标签: php wordpress post random

嗯,听起来很复杂

我有这些代码

// counts words abd do something if words > 20
$words == 20; // number of characters to compare
if(str_word_count(get_the_content(),0)  < $words){
the_content();
}else{
the_content('Continue Reading...')
}

//codex wordpress code for showing random posts
<ul>
<?php
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach ( $rand_posts as $post ) : 
  setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; 
wp_reset_postdata(); ?>
</ul>


//code for placing adsense after paragraph 1
<?php
$paragraphAfter= 1;
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++) {
if ($i == $paragraphAfter) { ?>
ad code
<?php
}
echo $content[$i] . "</p>";
} ?>

从这3个代码我想做出类似的东西

如果txt =&gt; 100个单词 在段落= 2添加 否则什么都不做


使用这些代码(如果是长帖)我可以减少反弹

1 个答案:

答案 0 :(得分:0)

if($i == 1 && strlen($content[$i]) >= 100)
{
    //your code
}

strlen function