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.
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.
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:
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');
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>