2017-08-29 47 views
1

我试图从我的网站的/ blog目录的HTML/CSS首页添加3个最近的博客文章。到目前为止它可行,但我遇到了问题;只要文章中的摘录限制较低,就不会显示“继续阅读”。我想知道是否可以通过首页上的PHP改变摘录限制。最近的WordPress的首页(HTML)文章

下面是当前的代码PHP:

<div class="ro-section ro-padding-top"> 
    <h3 class="ro-hr-heading">Our Latest Blog Articles</h3> 
    <div class="container"> 
     <?php 
      include('blog/wp-load.php'); 
      $args = array('showposts' => 6); 
      $the_query = new WP_Query($args); 

      echo '<div class="row">'; 

      if($the_query->have_posts()): 
      while ($the_query->have_posts()) : $the_query->the_post(); 

       echo '<div class="col-md-4"> 
       <h4 class="ro-service-item-4">'.get_the_title().'</h4> 
       <a href="'.get_the_permalink().'">'.get_the_post_thumbnail().'</a> 
       '.get_the_excerpt($limit).'</p></div>'; 

      endwhile; 
      endif; 

      echo '</div>'; 

      wp_reset_query(); 
     ?> 
    </div> 
    </div> 
+1

简短的回答:**是**。长的回答:你是否曾经使用过这个? Google“WordPress WP_Query”,看看你找到了什么。 –

+0

@cale_b是的我在Google上看过。我尝试添加wpdocs_custom_excerpt_length,但不知道如何完全实现它。仍在学习PHP。 – Emanuel

+0

向我们展示您为自定义摘录试用的代码 – FluffyKitten

回答

1

我想通了谷歌搜索和在线阅读之后。首先,你需要创建一个自定义功能:

 function get_excerpt(){ 
     $excerpt = get_the_content(); 
     $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); 
     $excerpt = strip_shortcodes($excerpt); 
     $excerpt = strip_tags($excerpt); 
     $excerpt = substr($excerpt, 0, 100); 
     $excerpt = substr($excerpt, 0, strripos($excerpt, " ")); 
     $excerpt = trim(preg_replace('/\s+/', ' ', $excerpt)); 
     $excerpt = $excerpt.'...'; 
     return $excerpt; 
     } 

我也不得不改变“.get_the_excerpt($限制)。”到'.get_excerpt()'中。因为自定义函数已被重命名。

下面是代码一起:

<div class="ro-section ro-padding-top"> 
<h3 class="ro-hr-heading">Our Latest Blog Articles</h3> 
<div class="container"> 
    <?php 
     include('blog/wp-load.php'); 
     $args = array('showposts' => 3); 
     $the_query = new WP_Query($args); 

     function get_excerpt(){ 
     $excerpt = get_the_content(); 
     $excerpt = preg_replace(" (\[.*?\])",'',$excerpt); 
     $excerpt = strip_shortcodes($excerpt); 
     $excerpt = strip_tags($excerpt); 
     $excerpt = substr($excerpt, 0, 100); 
     $excerpt = substr($excerpt, 0, strripos($excerpt, " ")); 
     $excerpt = trim(preg_replace('/\s+/', ' ', $excerpt)); 
     $excerpt = $excerpt.'...'; 
     return $excerpt; 
     } 

     echo '<div class="row">'; 

     if($the_query->have_posts()): 
     while ($the_query->have_posts()) : $the_query->the_post(); 

      echo '<div class="col-md-4"> 
      <h4 class="ro-service-item-4">'.get_the_title().'</h4> 
      <a href="'.get_the_permalink().'">'.get_the_post_thumbnail().'</a> 
      '.get_excerpt().'<br /><br /> 
      <a href="'.get_the_permalink().'"><button type="button" class="btn btn-read_more center-block">Read the article</button></a> 
      </div>'; 

     endwhile; 
     endif; 

     echo '</div><br /><br /><br />'; 

     wp_reset_query(); 
    ?> 
</div>