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

Lydia Roberts
Lydia Roberts
3,535 Points

WordPress Archive page title not printing

Using Zac's Bootstrap to WordPress course, I've created a very custom theme and fleshed it out a good deal.

I run into trouble with the Archive (archive.php) template and the Search Results page (search.php). Neither one prints the title of the page correctly.

The archive page prints nothing - totally blank. I'd like it to print the name of the category/tag/date.

The searchform page prints the title of the first page/post. I'd like it to print "Search results for: [name of search term]"

What is the correct code needed to print the page titles? I've tried lifting bits of code from base templates like twentyfifteen but not getting any correct results. I'm kind of a php beginner. Thanks!

1 Answer

Jason Campbell
Jason Campbell
9,822 Points

Hi Lydia, I haven't taken that course yet but I have developed in WP quite a bit. The function the_title() may work for that. Or you can search around the WP codex if that doesn't work.

http://codex.wordpress.org/Function_Reference/the_title

Hope that helps. -Jason

Lydia Roberts
Lydia Roberts
3,535 Points

Thanks for your reply, Jason. That function doesn't quite cover these scenarios but I was able to search around and cobble together the following which works well for archive.php:

        ```
 <h1>
<?php if ( is_day() ) : ?>
    <?php printf( __( '<span>Blog Archives:</span> <em>%s</em>' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : ?>
    <?php printf( __( '<span>Blog Archives:</span> <em>%s</em>' ), get_the_date( _x( 'F Y', 'monthly archives date format', '' ) ) ); ?>
<?php elseif ( is_year() ) : ?>
    <?php printf( __( '<span>Blog Archives:</span> <em>%s</em>'), get_the_date( _x( 'Y', 'yearly archives date format', '' ) ) ); ?>
<?php elseif ( is_category() ) : ?>
    <?php printf( __( '<span>Blog Category:</span> <em>%s</em>' ), '' . single_cat_title( '', false ) ); ?>
    <?php echo category_description(); ?>
<?php elseif ( is_tag() ) : ?>
    <?php printf( __( '<span>Blog Tag:</span> <em>%s</em>' ), '' . single_tag_title( '', false ) ); ?>
<?php else : ?>
    Blog Archives
<?php endif; ?>
 </h1>
        ```

I hope that might help others. Basically the above will print out different things based on if it's an archive by day, month, year, category, or tag.

I haven't figured out the right format for the Search page yet.