• To avoid excerpts to display when they are empty, I use the following filter in functions.php that works perfectly:

    remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );

    However, I would like to remove empty excerpts only for the archive pages of my custom taxonomy (not the blog or others). I tried the following code since is a custom category, but it does not work:

    if ( is_tax('custom-taxonomy') ) {
        remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' ); 
    }

    What am I missing, any idea?

    • This topic was modified 3 years, 7 months ago by mixali.
Viewing 8 replies - 1 through 8 (of 8 total)
  • That code should work for the archive page of the taxonomy, but not for anywhere else that the post is shown.
    https://developer.www.ads-software.com/reference/functions/is_tax/

    Thread Starter mixali

    (@mixali)

    Thanks Joy,
    Actually that is what I want, to apply the filter only on my custom taxonomy pages, but somehow it does nothing.

    Where did you put your code? The concern is order of execution.
    Can you try it with an echo instead of a filter, just to see if it is matching?

    Thread Starter mixali

    (@mixali)

    That’s a good point Joy. I tried the echo instead of the filter but I do not get anything.

    Instead, I tried too to use the conditional if ( is_singular( 'portfolio' ) ) but is not working, even if I’m pretty sure that this page post type is “portfolio”.

    Any clue what is going in my portfolio?

    You didn’t answer “where did you put your code?”
    If it’s straight in functions.php, that’s not going to work. If it’s in a function, when is that function run? If it’s in a template, that would affect only that template.
    The query can’t match is_tax() at the same time as is_singular() since one is archive and one is single.

    Thread Starter mixali

    (@mixali)

    Thank you Joy,
    As I said in my first message I am using this on functions.php file of my child theme, for example as:

    function remove_empty_excerpt() {
    	if ( is_singular( 'portfolio' ) ) {
    		remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
    	}
    }

    When I use there the following function:

    add_action('wp_footer', 'your_function');
    function your_function() {
    	$post_type = get_post_type( $post->ID );
    	echo $post_type;
    }

    I get the string “portfolio”, that is correct.

    Am I missing a “add_action” on the first one? What should be the first argument?

    Thanks in advance,

    Thread Starter mixali

    (@mixali)

    Thanks, Obviously I need a add_action but I am not sure what to use, i.e.

    function remove_empty_excerpt() {
    	if ( is_singular( 'portfolio' ) ) {
    		remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
    	}
    }
    add_filter( 'the_content', 'remove_empty_excerpt' );

    This works definitively on my portfolio pages removing empty excerpts, but it will remove any content on any other post type.I need to ad an exception with an “else” for any other post type, right?

    Although actions and filters are “the same thing”, actions don’t return data but filters must return data. It’s not good to mix their usage.

    Take a look at https://codex.www.ads-software.com/Plugin_API/Action_Reference and choose an action that allows the filter for excerpt to have been set up, but before the excerpt is output.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘remove empty Excerpt in Custom Taxonomy archive pages’ is closed to new replies.