views:

19

answers:

1

Hello,

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.

How to check this. Is there any conditional tag

+1  A: 

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().

John P Bloch
oh thank you very much ! Looks like you code will work. I too tried this method but instead of defining i added a text and searched for that text. But my xampp got hanged after doing that. I will definitely try this. Since i am on a plugin dev the fopen method is very expensive like !
Aakash Chakravarthy