du6333137 2017-07-17 12:24
浏览 35
已采纳

仅在首页上显示WordPress帖子的第一句作为“突出显示的帖子”部分

I'm currently working on my first WordPress theme and I want to create a "Highlighted Post" section on my index.php. There are a lot of scripts for this but all seem to apply a permanent filter over the entire index.php. That is problematic since i want to show the whole posts on my index.php below. Is there a way to apply this special filter only for this section and not for the whole index.php?

  • 写回答

1条回答 默认 最新

  • dps43633 2017-07-17 12:58
    关注

    If you could give us more info what determines this "Highlighted" posts we might be able to be more specific in solving this.

    But in the mean time I'll guess it's two latest posts which means you could have two queries in the index.php

    First one would be for "Highlighted" ones:

    <?php
      $args = array( 'numberposts' => 2 );
      $lastposts = get_posts( $args );
      foreach($lastposts as $post) : setup_postdata($post); 
    ?> 
    
      <h2 class="news"><?php the_title(); ?></h2>
      <?php the_excerpt(); ?>
    
    <?php endforeach; ?>
    

    This will give you excerpt of your post in this query.

    What you could do then is limit excerpt to whatever length you'd like and add read more link if you want :)

    function newExcerpt($more) {
       global $post;
       return '... <a href="'. get_permalink($post->ID) . '">Read more</a>';
    }
    add_filter('excerpt_more', 'newExcerptReadMore');
    
    
    function customExcerptLength( $length ) {
        return 20;
    }
    add_filter( 'excerpt_length', 'customExcerptLength', 999 );
    

    And for other posts you could do another query but not using first two posts like this:

    $count = 0;
    $lastposts = get_posts();
    foreach($lastposts as $post) : setup_postdata($post); 
    if($count > 1)
    ?> 
    
      <h2 class="news"><?php the_title(); ?></h2>
      <?php the_excerpt(); ?>
      $counter++;
    
    <?php endforeach; ?>
    

    This will just loop through the posts and skip first two posts. If you have some other term or category or something else that determine Highlighted posts then you can use that in and pass it as an argument to get_posts($args).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画