2017-08-29 9 views
1

私のサイトの/ blogディレクトリにあるHTML/CSSのフロントページに3つの最近のブログ記事を追加しようとしています。これまでのところ動作しますが、Imは問題にぶつかります。記事の抜粋の制限が低いときはいつでも、 "読み続ける"と表示されません。私は何とかフロントページの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> 

関連する問題