Reputation: 952
I am building a child theme in Wordpress that needs some custom functionality (for an example, I want to show an author bio on things in the 'fiction' category, but not in the 'fiction-contest' category, though they otherwise look the same).
It seems that no matter how specific my templates are, eventually I run into one of two things:
the_content();
or
get_template_part( 'content', get_post_format() );
This is the part I want to have finer control over. Yet it seems I can only grab content as a big chunk. My workaround has been to style parts of the content as "display: none", depending on the template being used, but is there a way to conditionally change actual content returned by the above code? If so, how and where would I do that?
Upvotes: 3
Views: 1468
Reputation: 5700
the_content() should return only the post contents. The author information should not be included unless the author explicitly listed it within the post.
I believe you are looking for something along the lines of:
<?php
// This array can contain category IDs, names, or slugs.
// I went with an array for the ability to easily extend in the future.
$cats_for_bio = array(
"Fiction",
"OtherCategory"
);
if ( in_category($cats_for_bio) ) {
// You need to get the author id since we are outside of the loop.
$authorID = $posts[0]->post_author;
// Place your bio output code here as you please.
// This example just grabs the author name and description.
$authorName = get_the_author_meta("first_name", $authorID) . " ";
$authorName .= get_the_author_meta("last_name", $authorID);
$authorDesc = get_the_author_meta("user_description", $authorID);
$authorBio = "<p><b>Author:</b> $authorName</p>/n";
$authorBio .= "<p><b>Bio:</b> $authorDesc</p>/n";
echo $authorBio;
}
?>
Being that you are creating your own theme you can easily use the function get_the_content()... This will give you the content without echoing it... the downside of this is the filters that apply to the_content() are no longer applied. You can get the content with the filters from the get_the_content() function by using apply_filters('the_content', $content) with $content being the return variable sent by get_the_content()
A third option which was previously mentioned is to create your own filter... this would be best done in a plugin and would be able to be applied to any theme you may use in the future.
Upvotes: 2
Reputation: 52372
Either:
Don't put the author bio in the post content (use a Custom Field for example) and display it when appropriate. You don't have to touch the content then.
Write a filter for the_content
that changes its content when you want.
http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
Upvotes: 3