How To Change The Excerpt Length In WordPress (With And Without A Plugin)

Featured Imgs 21

In many WordPress themes blog posts display a brief excerpt instead of the full text. These themes utilize the WordPress function the_excerpt() in their files, which then renders the first 55 words of the post by default, unless a manual excerpt is entered in the corresponding field for the post. In certain situations you may wish to have more control of what is displayed, such as changing the excerpt length. In this post we will show you how to change the excerpt length with or without a plugin.

UNLIMITED DOWNLOADS: 500,000+ WordPress & Design Assets

Sign up for Envato Elements and get unlimited downloads starting at only $16.50 per month!




How To Change The Excerpt Length In WordPress Without A Plugin

Changing the excerpt length without a plugin is relatively simple to do, even if you have little or no coding knowledge. However, the only control this gives you is over the number of words in the excerpt, nothing more. For more control options, we recommend using the plugin discussed in the next section.

Place the following code at the bottom of your theme’s functions.php file:

add_filter( 'excerpt_length', '1wd_excerpt_length' );
function your_prefix_excerpt_length() {
    return 30;
}

Simply change the number 30 in the code to the number of words you prefer.

How To Change The Excerpt Length In WordPress With A Plugin

For much more control over the excerpt, we recommend using The Advanced Excerpt plugin. After installing this plugin you can set it up relatively easily – and without any code – to:

  1. Keep HTML markup in the excerpt (and you get to choose which tags are included)
  2. Trim the excerpt to a given length using either character count or word count
  3. Only the ‘real’ text is counted (HTML is ignored but kept)
  4. Customize the excerpt length and the ellipsis character that are used
  5. Complete the last word or sentence in an excerpt (no weird cuts)
  6. Add or remove a read-more link to the text
  7. Ignore custom excerpts and use the generated one instead
  8. Developers can use the_advanced_excerpt() for even more control (see the FAQ)

There are other plugins with similar functionality that you can use but we have used this one in hundreds of websites for many years, so we can confidently recommend it.

Now you know how to gain more control over the excerpt in your WordPress website. We hope you have found this quick tip helpful for your future projects! Be sure to check out our other WordPress tutorials while you’re at it.

How To Limit WordPress Search Results By Post Type

Featured Imgs 21

There are occasions in which you may want to limit WordPress search results to a specific post type rather than just the default. Your theme may have custom post types that you want to be included in the search, or perhaps you only want to search the custom post type and not posts or pages. This is relatively simple to do without a plugin, which is usually best practice when possible.

UNLIMITED DOWNLOADS: 500,000+ WordPress & Design Assets

Sign up for Envato Elements and get unlimited downloads starting at only $16.50 per month!




 

Open your theme’s functions.php file and copy the following code snippet at the bottom.

function 1wd_searchfilter($query) { 
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('post','page'));
    } 
return $query;
} 
add_filter('pre_get_posts','1wd_searchfilter');

Note the line where it says

$query->set('post_type',array('post','page'));

Change that line where it says ‘post’, ‘page’ to whatever post type you want the search to be filtered through. So, for example, your code would look like this:

function 1wd_searchfilter($query) { 
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('custom_post_type_name_1','custom_post_type_name_2'));
    } 
return $query;
} 
add_filter('pre_get_posts','1wd_searchfilter');

Be sure to change the name of the custom post type to your own.

We hope that helps you next time you need to limit WordPress search results by post type. Be sure to check out our other WordPress tutorials for more useful tips!

How To Disable RSS Feeds In WordPress

Featured Imgs 21

By default, there is no option to disable RSS feeds in WordPress, because RSS feeds allow users to subscribe to your blog posts. However, there are certain situations where you may want to turn off RSS feeds. There are a few plugins that you can use to do this, but whenever possible we always try to use as few plugins as possible and use our own code.

UNLIMITED DOWNLOADS: 500,000+ WordPress & Design Assets

Sign up for Envato Elements and get unlimited downloads starting at only $16.50 per month!




 

By placing the following code in your theme’s functions.php file (or a site-specific plugin) when someone tries to go to the RSS feed link (yoursite.com/feed/) they will receive a message that no feed is available.

function 1wd_disable_feed() {
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}

add_action('do_feed', '1wd_disable_feed', 1);
add_action('do_feed_rdf', '1wd_disable_feed', 1);
add_action('do_feed_rss', '1wd_disable_feed', 1);
add_action('do_feed_rss2', '1wd_disable_feed', 1);
add_action('do_feed_atom', '1wd_disable_feed', 1);
add_action('do_feed_rss2_comments', '1wd_disable_feed', 1);
add_action('do_feed_atom_comments', '1wd_disable_feed', 1);

Now you know how to disable RSS feeds in WordPress. Be sure to check out our other WordPress tutorials for more tips!