• Hi guys,

    I have a category called the societies guide on my website, https://www.corkstudentnews.com, which is a college news site.

    Each post in the category is a profile of a particular society.

    What I want to do is add certain content (automatically) at the beginning of each post.

    One item would be a drop down menu (php controlled) and the other (possibly) would be a logo for the guide.

    I know this is probably possible using some fancy PHP, but I can’t seem to find any info online.

    Any help would be appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Simple filter should do it..

    function print_extra_category_content( $content ) {
    	if( is_category() )
    		echo '<h1>Test extra category content</h1>';
    	return $content;
    }
    add_filter( 'the_content', 'print_extra_category_content' );

    NOTE: If you attempt to return the extra content WordPress will strip out the HTML, but echoing the additional content avoids that issue..

    If you meant when viewing the individual category posts, you’ll need a little tweak to the above, something like..

    function print_extra_category_content( $content ) {
    	if( is_single() && in_category( 4 ) )
    		echo '<h1>Test extra category content</h1>';
    	return $content;
    }
    add_filter( 'the_content', 'print_extra_category_content' );

    Where 4 should be the ID of the appropriate category.

    Hope that helps.. ??

    ooh looks fancy. thanks

    understand the php but where do i add it to?

    The functions.php file of your theme would be fine. The alternative would be to make it into a plugin, not really worth it for a few lines of code, unless you’re likely to change theme.

    Code should be placed on a new line after the very first <?php at the top of the file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Automatically add content in category posts.’ is closed to new replies.