Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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' ) )
share|improve this answer
    
That fixes it from showing up on pages but now I can't see it on the home page either. It is only showing the bio for single blog posts. –  jmreicha Nov 4 '12 at 21:21
    
Nevermind, it looks like the is_front_page did the trick. Thank you! –  jmreicha Nov 4 '12 at 21:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.