WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#39921 reopened enhancement

the_excerpt needs additional parameters to control teaser length and visibility of 'read more' link

Reported by: subrataemfluence Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.2
Component: Posts, Post Types Keywords: close
Focuses: template Cc:
PR Number:

Description

Function the_excerpts() need additional parameters to control teaser length and visibility of "read more" link. Applying filters in functions.php in order to customise 'read more' link or teaser length make it pretty generic.

If the function can look like this:
the_excerpt($length, $moreLink = true) and we can pass these values as and when required the function would become more useful!

Where this could be useful? Here is an example (screenshot attached):

In my site, users can post their trip report from front end via a custom post type. Admin publishes a post and decides whether or not to make the post visible on the site immediately (by 'Scheduling a post).

There is a page where all "Published" posts are listed (scheduled "today") and a right sidebar to list 10 posts which are "scheduled" for a later date (Upcoming Stories).

Such posts are listed with their title and excerpt but there would be no link for users to view the details. The posts are listed under Upcoming for better indexing and encouraging people to come back to the site more often to read new stories.

The requirement is in under Upcoming Stories I want to put say 20 words without a read more link whereas the list (on the same page) in the content area would have bigger teaser (say 50 words) along with 'read more` link.

The sidebar for Upcoming Stories is a custom plugin and placed inside a wizard while the main content is using a template page.

My intention is to do something like this:

In template code I would write:
the_excerpt(50)

and inside the plugin code I would wish to write:
the_excerpt(20, false)

I can hide read more link via CSS but it will not stop the URL from rendering. It will still be visible in page view source. Any scraping script would be able to find the these links easily!

NB: In the screenshot, I have made read more link hidden in sidebar using CSS to explain what I am trying to say. The main content area list however displays the link. It would be great if this could be made using server side scripting like I mentioned above by adding two additional parameters in the function.

Attachments (1)

Screenshot from 2017-02-20 21:53:59.png (242.0 KB) - added by subrataemfluence 3 years ago.

Download all attachments as: .zip

Change History (7)

#2 @grapplerulrich
3 years ago

  • Component changed from Customize to Posts, Post Types
  • Focuses template added; performance removed
  • Keywords close added
  • Resolution set to invalid
  • Status changed from new to closed

There are two filters which can be used for this task

https://developer.wordpress.org/reference/hooks/excerpt_length/ and https://developer.wordpress.org/reference/hooks/excerpt_more/

If a different setting is needed for different elements then a conditional statement can be used within the filters.

#3 @subrataemfluence
3 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Sorry for following up!

What is the harm if we introduce two additional parameters? This will definitely give developers more control.

Now regarding apply_filters and using a hook (excerpt_length) to a specific function, we can not use the same hook with a different function with different settings. Only one will be in action. Am I right?

But if we have parameters introduced in the core method we then have the ability to pass value to it directly without writing additional function for customised output and hook it.

According to what your said ("If a different setting is needed for different elements then a conditional statement can be used within the filters.") - if I take my above scenario as a use case, how I do this? What conditional statement/s I should use within the filters?

Please don't think I am asking "how can I ..."!

#4 @grapplerulrich
3 years ago

<?php
add_filter( 'excerpt_length', function( $number ) {
        if ( is_main_query() } {
                $number =  50;
        } else {
                $number = 20;
        }
        return $number;
} );

There is whole list of conditional tags. https://codex.wordpress.org/Conditional_Tags

#5 @subrataemfluence
3 years ago

Thank you! https://codex.wordpress.org/Conditional_Tags is a very useful area to explore.

Meanwhile I added the following function in functions.php which actually works, although probably this is not the correct way to go!

function get_excerpt($limit, $show_more = true) {
        $excerpt = explode(' ', get_the_excerpt(), $limit);
        if (count($excerpt)>=$limit) {
            array_pop($excerpt);
            $excerpt = implode(" ",$excerpt).'...';
        } else {
            $excerpt = implode(" ",$excerpt);
        }
        $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
        if($show_more)
            return $excerpt . '<a href="'.get_the_permalink().'" rel="nofollow">&nbsp;[more]</a>';
        else
            return $excerpt;
    }

#6 @grapplerulrich
3 years ago

There is function that reduces the number of words https://developer.wordpress.org/reference/functions/wp_trim_words/

WordPress runs a few other functions before outputting the excerpt. https://github.com/WordPress/WordPress/blob/master/wp-includes/default-filters.php#L143-L148

Note: See TracTickets for help on using tickets.