0

I'm trying to add some static (welcome) text above my post excerpts, for which the posts page has been set up to display a category, in this case News. Link

I've tried entering the following:

<?php echo category_description(); ?>

into the index.php and then adding a category description, and it sort of works, but I end up with a huge white, empty space below the static text, and before the first blog excerpt. Any help will be greatly appreciated.

0

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:

  • Thanks, but I'm afraid much of that went over my head. – Ken A Dec 4 '15 at 17:25
  • @KenA added comments, and link to functions documentation – gmazzap Dec 4 '15 at 17:36
0

Category descriptions are formatted in paragraphs, which can add unnecessary white space.

(By looking at your page I see this inline style:

<p style="min-height: 3616px;">

This is causing the problem. You should find and correct this parameter. Since it is inline, it is not in your style.css file, but in your .php files where you call the specific text.)

Solution

Go where you added the

<?php echo category_description(); ?>

Replace it with this

<div class="my-term-description">
<?php echo term_description(); ?>
</div>

Then go to your functions.php file and add this at the bottom

remove_filter('term_description','wpautop');
  • Thanks, I added what you suggested to functions.php and nothing happened. – Ken A Dec 4 '15 at 14:07
  • Thanks, I've been trying to find out where <p style="min-height: 3616px;"> is. – Ken A Dec 4 '15 at 22:52
  • Tried again this morning, and I get <p style="min-height: 3459px;">. Still can't find where to edit it though. – Ken A Dec 5 '15 at 7:07
  • @KenA Please read my updated answer and if correct mark it so. – IXN Dec 5 '15 at 11:54
  • Thanks but no joy I'm afraid, it's not made any difference. – Ken A Dec 6 '15 at 13:55
0

If anyone is interested, I have used the following solution:

<div id="cat-desc"><?php echo category_description(); ?></div>
<style>#cat-desc{ min-height: 0px !important; }</style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.