对自定义WP_Query设置字符限制

对自定义WP_Query设置字符限制

问题描述:

我正在为此寻找解决方案已经有2天了,但直到现在,没有任何答案帮助我解决问题。对自定义WP_Query设置字符限制

我有一个WP_Query的自定义Wordpress循环,我只想在the_content中显示只有100个字符或更少(不是单词)的帖子。

<?php $movies = new WP_Query(
    array(
     'showposts' => 4, 
    ) 
); 
if($movies->have_posts()) : while($movies->have_posts()) : $movies->the_post(); ?> 

     <h2><?php the_title(); ?></h2> 
     <div id="content"><?php the_content(); ?></div> 

<?php endwhile; endif; ?> 

是否有任何核心解决方案,我可以用作一个数组或我需要找出另一种方法来做到这一点?

我如何获得这些结果?

谢谢你们,你太棒了。

+0

你的意思,你只需要搜索具有在内容100个字符,或者你想要得到任何职位,但只显示100个字符的帖子(即切断多余的内容)?这听起来像你想要的第一个,但它没有多大意义,所以我假设你真的想截断长度为100个字符? – FluffyKitten

+0

感谢您的评论@FluffyKitten。是的,我想要第一个。搜索具有100个或更少字符的帖子,并忽略具有101个或更多字符的其他帖子。 – caiopereira

+0

我不确定这甚至可以通过Wordpress。您可能需要编写自定义SQL查询来直接查询数据库。 – FluffyKitten

试试这个

<?php $movies = new WP_Query(
    array(
     'showposts' => 4, 
    ) 
); 
if($movies->have_posts()) : while($movies->have_posts()) : $movies->the_post(); ?> 

     <?php if(strlen(get_the_content()) <= 100): ?> 

     <h2><?php the_title(); ?></h2> 
     <div id="content"><?php the_content(); ?></div> 
    <?php endif; ?> 

<?php endwhile; endif; ?> 
+0

它像一个魅力。谢谢。 – caiopereira

+0

这实际上并不像OP想要的那样工作......你只得到4篇文章,然后只显示 100个字符,它实际上不会显示任何内容。 – FluffyKitten

+0

你错了,OP也表示它按照他想要的方式工作。你不能拒绝投票,因为那... –