WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Yet Another Related Posts Plugin] Solutions: Excerpt length & removing <p> tag (2 posts)

  1. investor
    Member
    Posted 4 years ago #

    (1) Excerpt length:

    YARPP lets you control the excerpt length through the admin dashboard. However, this has two limitations. (i) It doesn’t apply to custom excerpts and (ii) it is disabled if you opt for a custom template file.

    The problem is severe because if you have a 200 word excerpt, the whole of it shows up spoiling the layout.

    Changing the excerpt length at a global level may not be feasible because you may want the long excerpt for other purposes.

    The solution is this (works on WP 2.8.4):

    (i) In your theme functions.php file (create it if not there), add the following:

    function theme_excerpt($num=55) {
        $link = get_permalink();
        $limit = $num;
        $excerpt = explode(' ', strip_tags(get_the_excerpt()), $limit);
        if (count($excerpt)>=$limit) {
            array_pop($excerpt);
            $excerpt = implode(" ",$excerpt).'...';
        } else {
            $excerpt = implode(" ",$excerpt);
        }
        $excerpt = apply_filters('the_content', $excerpt);
        $excerpt = preg_replace('<code>\[[^\]]*\]</code>','',$excerpt);
        echo $excerpt;
    }

    (ii) In the YARPP template file, where it says:

    <?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><!-- (<?php the_score(); ?>)--></li>

    add the words

    <?php theme_excerpt('50') ; ?> before the </li> tag.

    Change the number to whatever you like.

    You can customize the output with css.

    (Code copied from http://bavotasan.com/tips/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/#comment-4066 and used for YARPP)

    (2) Removing the <p> tag from the extract.

    In the default mode, a <p> tag is not added to the excerpt making the excerpt flow after the title.

    However, in the template mode, WP’s default behaviour prevails and a <p> tag is added causing the title and excerpt to be separated by a line.

    You may not want to change things at a global level.

    So the solution is to disable the effect of the <p> tag using css.

    Create a new class, say ‘yarppexcerpt’ in your css file with the following properties:

    . yarppexcerpt p {margin: 0px !important; display: inline ;}

    and the excerpt flows after the title.

    Hope somebody finds this useful.

    Regards.

  2. @investor - very cool! Thanks for sharing this code with others. I'm sure this would also be helpful for others who need to customize their excerpting function in different ways (for no-space languages, etc.).

Topic Closed

This topic has been closed to new replies.

About this Topic