0

I know this is normally an obvious question with a lot of answers (like here), but I'd like to show only the beginning of the posts with a "read more" button. I don't use excerpts, so I'd like Wordpress to automaticly grab the first 55 words or the first phrase to display it.

I use the theme Toivo Lite. The index.php is:

<?php
get_header(); ?>

<?php if ( have_posts() ) : ?>

    <?php do_action( 'toivo_before_loop' ); // Action hook before loop. ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'content', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) );
        ?>

    <?php endwhile; ?>

    <?php
        the_posts_pagination( array(
            'prev_text'          => __( 'Previous page', 'toivo-lite' ),
            'next_text'          => __( 'Next page', 'toivo-lite' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'toivo-lite' ) . ' </span>',
        ) );
    ?>

<?php else : ?>

    <?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>

<?php do_action( 'toivo_after_loop' ); // Action hook after loop. ?>

<?php get_footer(); ?>

I've tried to replace the two content by excerpt, but then the page is empty (only header and foot).

2 Answers 2

0

Try replacing this:

        <?php
            /* Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'content', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) );
        ?>

With this:

<?php echo my_excerpt(get_the_ID(), 300, 'Read More'); ?>

And then add this to your functions.php file:

function my_excerpt($postid, $len = 500, $more = false) {

    $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $postid));

    if(empty($excerpt)) $excerpt = strip_shortcodes(strip_tags(apply_filters('the_excerpt', get_post_field('post_content', $postid))));

    if(strlen($excerpt) > $len) $excerpt = substr($excerpt, 0, $len-3) . '&hellip;';

    if($more) $excerpt .= '<a class="read-more" href="' . get_the_permalink($postid) . '">' . $more . '</a>';

    return $excerpt;
}

By default that will return the first 300 characters of the post content. Change the "300" in the my_excerpt call to whatever number of characters you want to return (note, this is not how many words to return, but how many characters).

4
  • This is not working. The first 300 characters are displyed, but there isn't any formating nor titles... This is all in one block.
    – Shan-x
    Commented Mar 28, 2017 at 15:35
  • Yes, this code strips all that out. If you don't want it stripped out, then change this line: if(empty($excerpt)) $excerpt = strip_shortcodes(strip_tags(apply_filters('the_excerpt', get_post_field('post_content', $postid)))); to this instead: if(empty($excerpt)) $excerpt = apply_filters('the_excerpt', get_post_field('post_content', $postid)); Commented Mar 28, 2017 at 15:39
  • This is better, but there is still not titles or formating (take all width instead of only the column).
    – Shan-x
    Commented Mar 28, 2017 at 15:42
  • In that case, try this instead for that line: if(empty($excerpt)) $excerpt = get_post_field('post_content', $postid); that will spit out the content verbatim but could be dangerous because if there is any html returned at the beginning of the post it will be counted in the character count, potentially leading to different length content returned from post to post. Commented Mar 28, 2017 at 15:44
0

You can try the below:

    <?php
    get_header(); ?>

    <?php if ( have_posts() ) : ?>

        <?php do_action( 'toivo_before_loop' ); // Action hook before loop. ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php
            /* Include the Post-Format-specific template for the content.
            * If you want to override this in a child theme, then include a file
            * called content-___.php (where ___ is the Post Format name) and that will be used instead.
            */
            echo wp_trim_words( get_the_content(), $num_words = 50, $more = '...' );
            echo '<a href="'.get_the_permalink().'">Read More</a>';
            ?>

        <?php endwhile; ?>

        <?php
        the_posts_pagination( array(
        'prev_text'          => __( 'Previous page', 'toivo-lite' ),
        'next_text'          => __( 'Next page', 'toivo-lite' ),
        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'toivo-lite' ) . ' </span>',
        ) );
        ?>

    <?php else : ?>

        <?php get_template_part( 'content', 'none' ); ?>

    <?php endif; ?>

    <?php do_action( 'toivo_after_loop' ); // Action hook after loop. ?>

    <?php get_footer(); ?>

I have replaced:

    get_template_part( 'content', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) );

With:

    wp_trim_words( the_content(), $num_words = 50, $more = '...' );

Someone might tell me this is the wrong approach, but it might help you. It will display the first 50 words of the content with a "..." following it.

3
  • 1
    Where am I suppose to put that?
    – Shan-x
    Commented Mar 28, 2017 at 16:42
  • Please click the edit link below your answer add more information on where you can put this, along with any necessary code formatting.
    – Howdy_McGee
    Commented Mar 28, 2017 at 17:14
  • I have edited the code so that it makes more sense, hope this helps.
    – Joe
    Commented Mar 29, 2017 at 8:31

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.