Exclude custom function content from certain pages

I have added a small custom function to display a little author bio blurb at the bottom of each blog entry in my functions.php file inside the current theme that I am using. So far so good, it is working great but I noticed that this function is being applied to every page, for example it is showing up at the bottom of my “Contact” page.

Here is an example of what I’m talking about to give a visualization of what is happening:

Read More

enter image description here

Here is the code I am using for my function:

function get_author_bio ($content=''){

    global $post;

    $post_author_name=get_the_author_meta("display_name");
    $post_author_description=get_the_author_meta("description");
    $html.="<div class='author_text'>n";
    $html.="<h4>About the Author: <span>".$post_author_name."</span></h4>n";
    $html.= $post_author_description."n";
    $html.="</div>n";
    $html.="<div class='clear'></div>n";
    $content .= $html;

    return $content;
}

add_filter('the_content', 'get_author_bio');

How can I make this function only be applied to my home page for blog posts and not other pages on my site?

Hopefully that is enough to help me, but I can update this questions to provide any other information that you may need. Thanks.

Related posts

Leave a Reply

1 comment

  1. Check for the type of the page:

    function get_author_bio ($content=''){
    
        if ( ! is_single() ) // not a blog post, stop immediately
        {
            return $content;
        }
    
        global $post; // continue with regular work
    

    The easiest way to learn these check functions is a look at the function get_body_class(). Here are the most important:

    is_rtl()
    is_front_page()
    is_home()
    is_archive()
    is_date()
    is_search()
    is_paged() 
    is_attachment() 
    is_404() 
    is_single() 
    is_page() 
    is_singular() // same as is_page() and is_single()
    is_attachment() 
    is_archive() 
    is_post_type_archive() 
    is_author() 
    is_category() 
    is_tag() 
    is_tax() 
    is_user_logged_in() 
    is_admin_bar_showing() 
    

    You can even combine them:

    // search results, not the first page
    if ( is_search() and is_paged() )
    
    // attachment and user has probably a profile
    if ( is_attachment() and is_user_logged_in() ) 
    

    Some of these functions accept parameters to restrict the result:

    // contact page only
    if ( is_page( 'contact' ) )
    
    // this post was written by the tooth fairy
    if ( is_author( 'tooth-fairy' ) )