Skip to main content
Add comments, and link to docs
Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

You can use the the_excerpt and maybe also the_content filter hook, to catch when the excerpt is going to be print, and output your text before that.

Something likeThose filters are called just before the current post excerpt (or content) are printed to page.

Sample code:

/**
 * Function that runs when the excerpt for the post is going to be printed
 * and allow to edit it just in time
 *
 * @param string $excerpt Current post excerpt
 */
function prependNewsDescription( $excerpt ) {
  // let's check if we are in the archive for 'news'
  // and that we are looping main query
  if ( is_category( 'news' ) && in_the_loop() ) {
    // if so, let's get the description for current queried object
    // that is the "news" category term
    // also, remove any additional html tag
    $desc = wp_strip_all_tags( get_queried_object()->description, true );
    // finally, edit the excerpt prepending the description and a line break
    $excerpt = $desc . '<br>' . $excerpt;
  }

  // always return the (maybe edited) excerpt
  return $excerpt;
}


add_filter( 'the_excerpt', 'prependNewsDescription' );
add_filter( 'the_content', 'prependNewsDescription' );

This way you don't even need to edit your templates.

More info on the functions used:

You can use the the_excerpt and maybe also the_content filter hook, to catch when the excerpt is going to be print, and output your text before that.

Something like:

function prependNewsDescription( $excerpt ) {
  if ( is_category( 'news' ) && in_the_loop() ) {
    $desc = wp_strip_all_tags( get_queried_object()->description );
    $excerpt = $desc . '<br>' . $excerpt;
  }

  return $excerpt;
}


add_filter( 'the_excerpt', 'prependNewsDescription' );
add_filter( 'the_content', 'prependNewsDescription' );

This way you don't even need to edit your templates.

You can use the the_excerpt and maybe also the_content filter hook, to catch when the excerpt is going to be print, and output your text before that.

Those filters are called just before the current post excerpt (or content) are printed to page.

Sample code:

/**
 * Function that runs when the excerpt for the post is going to be printed
 * and allow to edit it just in time
 *
 * @param string $excerpt Current post excerpt
 */
function prependNewsDescription( $excerpt ) {
  // let's check if we are in the archive for 'news'
  // and that we are looping main query
  if ( is_category( 'news' ) && in_the_loop() ) {
    // if so, let's get the description for current queried object
    // that is the "news" category term
    // also, remove any additional html tag
    $desc = wp_strip_all_tags( get_queried_object()->description, true );
    // finally, edit the excerpt prepending the description and a line break
    $excerpt = $desc . '<br>' . $excerpt;
  }

  // always return the (maybe edited) excerpt
  return $excerpt;
}


add_filter( 'the_excerpt', 'prependNewsDescription' );
add_filter( 'the_content', 'prependNewsDescription' );

This way you don't even need to edit your templates.

More info on the functions used:

Source Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

You can use the the_excerpt and maybe also the_content filter hook, to catch when the excerpt is going to be print, and output your text before that.

Something like:

function prependNewsDescription( $excerpt ) {
  if ( is_category( 'news' ) && in_the_loop() ) {
    $desc = wp_strip_all_tags( get_queried_object()->description );
    $excerpt = $desc . '<br>' . $excerpt;
  }

  return $excerpt;
}


add_filter( 'the_excerpt', 'prependNewsDescription' );
add_filter( 'the_content', 'prependNewsDescription' );

This way you don't even need to edit your templates.