0

I have created custom Post type. Now I am using excerpt to show some limited words in the archive page. but I am showing same posts on the homepage in a content slider (Owl Carousel) which is giving me problem cuz excerpt size.

  1. So, I have decided to use first 160 words of the post to show on the archive page. Instead of Excerpt

  2. Also, I have placed Read more Button and I would like to know how to put the link in that

.

 <?php
 /**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package
 */

 ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<div class="row">

        <div class="col-md-3">
            <div class="thumbnail alignleft" style="width:100%;">
                <?php echo get_the_post_thumbnail(); ?>
          </div>
        </div>

        <div class="col-md-9">
                    <header class="entry-header article">
                        <?php
                        if ( is_singular() ) :
                            the_title( '<h1 class="entry-title">', '</h1>' );
                        else :
                            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
                        endif;

                        if ( 'post' === get_post_type() ) : ?>
                        <div class="entry-meta">
                            <?php arcvertex_posted_on(); ?>
                        </div><!-- .entry-meta -->
                        <?php
                        endif; ?>
                    </header><!-- .entry-header -->

                    <div class="row">
                        <div class="col-md-12">

                            <div class="entry-content-article">
                            <?php echo get_the_excerpt(); ?>
                            </div><!-- .entry-content -->

                            <button type="button" class="btn btn-readmore">Read More...</button>

                        </div>
                    </div><!-- .Row -->
        </div>
</div><!-- .Row -->

<hr class="half-rule-breadcrumb" style="margin-top:10px;" />

<footer class="entry-footer">
<?php arcvertex_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->
2
  • So the problems you are having are due to the excerpt not being long enough?
    – Cedon
    Commented Oct 14, 2017 at 18:59
  • No, I want to use words from the post instead of the excerpt. I am using the excerpt for another purpose. Commented Oct 16, 2017 at 5:49

2 Answers 2

0

WordPress has a helper function built into core, wp_trim_words(), that was added in v3.3.0.

In your case, simply replace the_excerpt() with echo wp_trim_words( get_the_content(), 80 ); where 80 is the number of words you want.

2
  • could you tell me how to link read more button to post? Commented Oct 17, 2017 at 13:42
  • I would just create an <a> element and style it to look like a button and have it point to esc_url( get_permalink() ) for the src= attribute.
    – Cedon
    Commented Oct 17, 2017 at 13:54
0

Add below function for character and word count in function.php

function wordlimit($string, $limit) 
{
    $overflow = true;
    $array = explode(" ", $string);
    $output = '';
    for ($i = 0; $i < $limit; $i++) 
    {
        if (isset($array[$i])) 
        {
            $output .= $array[$i] . " ";
        }
        else 
        {
            $overflow = false;
        }
    }

    return trim($output) . ($overflow === true ? "..." : '');
}
function charlimit($string,$limit){
    $string = preg_replace(" (\[.*?\])",'',$string);
    $string = strip_tags($string);
    $string = trim(preg_replace( '/\s+/', ' ', $string));
    return strlen($string)<=$limit?$string:substr($string,0,$limit).'...';
}

then use in your file like wordlimit(get_the_content( $post->ID ),160); instead of get_the_excerpt()

<?php
 /**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package arcvertex
 */

 ?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<div class="row">

        <div class="col-md-3">
            <div class="thumbnail alignleft" style="width:100%;">
                <?php echo get_the_post_thumbnail(); ?>
          </div>
        </div>

        <div class="col-md-9">
                    <header class="entry-header article">
                        <?php
                        if ( is_singular() ) :
                            the_title( '<h1 class="entry-title">', '</h1>' );
                        else :
                            the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
                        endif;

                        if ( 'post' === get_post_type() ) : ?>
                        <div class="entry-meta">
                            <?php arcvertex_posted_on(); ?>
                        </div><!-- .entry-meta -->
                        <?php
                        endif; ?>
                    </header><!-- .entry-header -->

                    <div class="row">
                        <div class="col-md-12">

                            <div class="entry-content-article">
                            <?php echo wordlimit(get_the_content( $post->ID ),160);?>
                            </div><!-- .entry-content -->

                            <button type="button" class="btn btn-readmore">Read More...</button>

                        </div>
                    </div><!-- .Row -->
        </div>
</div><!-- .Row -->

<hr class="half-rule-breadcrumb" style="margin-top:10px;" />

<footer class="entry-footer">
<?php arcvertex_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.