Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

John Dugan
PLUS
John Dugan
Courses Plus Student 13,165 Points

Title Block Function

Hi all, I am trying to create a simple function to output the name and description across posts, pages, category archives, taxonomy archives and custom post types.

Here is what works:

function my_page_title() {
    $term = get_queried_object();
    echo '<h1>' . __( $term->name, 'mysite') . '</h1>';
    echo '<p>' . __( $term->description, 'mysite') . '</p>';
}

However, I rather not print empty markup in the case where a description does not exist. When I wrap the 4th line in an if statement to check if the $term variable has a description I don't get anything though - even if there is a description associated with the page. I know the problem has something to do with the condition, but as a PHP novice I have not yet figured it out.

Any fixes to the function below?

function my_page_title() {
    $term = get_queried_object();
    echo '<h1>' . __( $term->name, 'mysite') . '</h1>';
    if ( $term->descripion ) {
        echo '<p>' . __( $term->description, 'mysite' ) . '</p>';
    }
}

2 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

You misspelled description in the conditional. Instead of this ...

if ( $term->descripion ) {

... you want this:

if ( $term->description ) {

Does that help?

John Dugan
John Dugan
Courses Plus Student 13,165 Points

Oh man... Epic fail. This isn't the first time I have been confounded by a misspelling. It certainly won't be the last... Thanks Randy.

Matt Campbell
Matt Campbell
9,767 Points

Couple of things to start with.

An else statement so we can see if the if statement is working and close the function so....

function my_page_title() {
$term = get_queried_object();
echo '<h1>' . __( $term->name, 'mysite') . '</h1>';
if ( $term->descripion ) {
    echo '<p>' . __( $term->description, 'mysite' ) . '</p>';
}else{
echo '<p>Nothing to return or whatever</p>;
} endif;
};

I don't know if that helps.