Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I conditionally change what is returned by the_content() in Wordpress?

Tags:

php

wordpress

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?

like image 864
David Rhoden Avatar asked Dec 31 '25 18:12

David Rhoden


1 Answers

Either:

  1. 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.

  2. Write a filter for the_content that changes its content when you want.

http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

like image 57
Dan Grossman Avatar answered Jan 02 '26 09:01

Dan Grossman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!