doudun2565 2014-09-17 18:24
浏览 22
已采纳

希望限制类别“页面”上的帖子大小

I'm looking to display the excerpt of posts in a Category being pulled into a custom page template.

As it sits now all category post pulled into the page are showing the entire post. I'd like to show roughly 40 words (or x number of characters) followed by "read more>>" excerpt.

Additionally I'd like to max out the number of posts displayed on the page to roughly 5 more or less.

 <?php while ( have_posts() ) : the_post(); ?>
 <?php get_template_part( 'content', 'page' ); ?>   

 <?php
 $catPost = get_posts('cat=225&posts_per_page=3');
 foreach ($catPost as $post) : setup_postdata($post); 
 ?>

 <?php get_template_part('content'); ?>
 <?php endforeach;?>
 <?php comments_template( '', true ); ?>
 <?php endwhile; // end of the loop. ?>
  • 写回答

2条回答 默认 最新

  • dongliu1883 2014-09-17 18:41
    关注

    Replace the_content() in content template by the following code

       $content = get_the_content();
       $trimmed_content = wp_trim_words( $content, 40, '<a href="'. get_permalink() .'"> ...' . Read More . '</a>' );
       echo trimmed_content;
    

    Or you can use the following code to your functions.php file

       function excerpt($num) {
        $limit = $num+1;
        $excerpt = explode(' ', get_the_excerpt(), $limit);
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt)." <a href='" .get_permalink($post->ID) ." ' class='".readmore."'>Read More</a>";
        echo $excerpt;
      }
    
    function excerpt_length($length) {
      return 40;
    }
    add_filter('excerpt_length', 'excerpt_length');
    

    Now replace the_content() by the_excerpt() in content template.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体