Check whether the_excerpt() function is called in a particular page

How to Check whether the_excerpt() function is called in a particular page example in a archive page.

The thing is i want to know whether the_excerpt() function is used to display the content or whether the_content() is used to display the content in archive/category pages.

Read More

How to check this. Is there any conditional tag

Related posts

Leave a Reply

1 comment

  1. There is no conditional tag, but there is a filter. If you hook into the 'get_the_excerpt' filter, your function will run. So, as an example:

    if(is_archive() || is_category()){
      add_filter( 'get_the_excerpt', 'my_custom_excerpt_filter' );
    }
    
    function my_custom_excerpt_filter( $excerpt ){
      if( !defined( 'USED_EXCERPT' ) ){
        define( 'USED_EXCERPT', true );
      }
    }
    

    Then, to check whether an excerpt was used, any time after the first iteration of the loop, use this:

    if(defined('USED_EXCERPT') && USED_EXCERPT){
      //If the excerpt was used, this if statement returns true.
    }
    

    This isn’t a very elegant or useful way of doing it, but this is how you would do it. If you need to know whether a template will use the excerpt before it loads the template, you’d have to hook this earlier, use fopen() to read through the template file and search for the_excerpt().