Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Last active March 17, 2022 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefuxia/2973189 to your computer and use it in GitHub Desktop.
Save thefuxia/2973189 to your computer and use it in GitHub Desktop.
Replace full content with excerpt
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Replace full content with excerpt
* Description: Replaces the full content on post listings with an excerpt
* Version: 2012.06.22
* Author: Fuxia Scholz
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
add_filter( 'the_content', 't5_replace_content_with_excerpt', 100 );
/**
* Return excerpt if we are not on a singular post view.
*
* @param string $content
* @return string
*/
function t5_replace_content_with_excerpt( $content )
{
if ( is_singular() )
{
return $content;
}
// remove our filter temporarily.
// Otherwise we run into a infinite loop in wp_trim_excerpt().
remove_filter( 'the_content', __FUNCTION__, 100 );
$excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );
add_filter( 'the_content', __FUNCTION__, 100 );
return $excerpt;
}
@KarmicP
Copy link

KarmicP commented Apr 23, 2020

Is it possibe to revert this to actually do it the other way? I need full posts versus excerpts on the homepage and of course theme customizer does not provide the option - thks (https://wordpress.stackexchange.com/questions/364907/custom-css-for-full-post-vs-excerpt-content-options)

@KarmicP
Copy link

KarmicP commented Apr 25, 2020

here is what I have to do exactly the contrary, placed in my child theme folder but it does not work

/**
 * Plugin Name: Replace full excerpt with content
 * Description: Replaces the excerpt on post listings with full content
 * Version:     2012.06.22
 * Author:      Fuxia Scholz <info@toscho.de>
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

add_filter( 'the_excerpt', 't5_replace_excerpt_with_content', 100 );

/**
 * Return content if we are not on a singular post view.
 *
 * @param  string $excerpt
 * @return string
 */
function t5_replace_excerpt_with_content( $excerpt )
{
	if ( is_singular() )
	{
		return $excerpt;
	}
	// remove our filter temporarily.
	// Otherwise we run into a infinite loop in wp_trim_excerpt().
	remove_filter( 'the_excerpt', __FUNCTION__, 100 );
	$content = apply_filters( 'the_content', get_the_content() );
	add_filter( 'the_excerpt', __FUNCTION__, 100 );
	return $content;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment