1
votes

Im trying to add the 3 recent blog articles on my HTML/CSS front page from the /blog directory of my site. So far it works, but Im running into a problem; Whenever the excerpt limit is low on an article it wont show "continue reading". I was wondering if I can somehow change the excerpt limit through PHP on the front page.

Here is the current code 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
Short answer: yes. Long answer: Have you even googled this? Google "WordPress WP_Query" and see what you find. - random_user_name
@cale_b Yes I've looked on Google. I tried adding a wpdocs_custom_excerpt_length but not sure how to fully implement it. Still learning PHP. - Emanuel
Show us the code you tried for the custom excerpt - FluffyKitten
@FluffyKitten I figured it out. Please take a look at the answer. Thank you for the encouragement. - Emanuel

1 Answers

1
votes

I figured it out after Googling and reading online. First you need to create a custom function:

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

I also had to change '.get_the_excerpt($limit).' into '.get_excerpt().' because the custom function was renamed.

Here is the code all together:

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