dtpxi88884 2017-09-03 11:42
浏览 46
已采纳

在自定义WP_Query上设置字符限制

I'm searching for a solution for this for 2 days already, but none of the answers helped me out until now.

I have a WP_Query for a custom Wordpress loop and I want to show only posts with 100 characters or less (not words) in the the_content.

<?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; ?>

Is there any core solution that I can use as an array or I need to figure out another way to do this?

How can I get those results?

Thank you guys, you're amazing.

  • 写回答

1条回答 默认 最新

  • dragon201401 2017-09-03 13:02
    关注

    Try this

    <?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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法