WordPress.org

Support

Support » Themes and Templates » Change ‘Blog Archive’ to ‘<category>’ in post titles?

Change ‘Blog Archive’ to ‘<category>’ in post titles?

  • I’ve found the bit in header.php that defines the title of a page, and got rid of the bit that says ‘blog archive’ like so:
    <title><?php bloginfo('name'); ?> <?php wp_title(); ?></title>
    but now I’d like to add a call for the page category before the <?php wp_title(); ?>. How can I do this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Can you better define “page category”?

    Do you mean Page (as in Write > Pages), or page (as in a general web page), or page (of category-query type content), or a post’s page?

    i meant post category, sorry. In the title of the ‘page’ (html page) it currently goes ‘blog name’ > ‘post title’ but i want to change it to ‘blog name’ > ‘category’ > ‘post title’
    (i already changed it from the default which said ‘title archives’ or something before the post title..) hope that clarified things..

    Hmm. What you’re trying to do is complicated a bit by the fact you can assign a post to multiple categories. Anyway, this bit of code (placed in your template before <title>) will scrape up the first (or only) category:

    <?php if(is_single()) {
    $post = $wp_query->post;
    $categories = get_the_category($post->ID);
    $cat_title = $categories[0]->cat_name;
    } ?>

    You may need to modify $cat_title= to incorporate “>” and whatnot. To display the category:

    <?php echo $cat_title; ?>

    Note that if you use multiple categories and wanted to display each, you could assign the category names to an array:

    <?php if(is_single()) {
    $post = $wp_query->post;
    $cat_titles = array();
    $categories = get_the_category($post->ID);
    foreach($categories as $category) {
    $cat_titles = $category->cat_name;
    }
    } ?>

    Then to display you could loop them in a foreach().

    in addition to avoid having Blog name.com >> >> category name (instead of Blog name.com >> category name) I changed the code to:

    <title><?php bloginfo(‘name’); ?> <?php if ( is_single() ) { ?> » <?php } ?>
    <?php if ( is_single() ) {
    $post = $wp_query->post;
    $categories = get_the_category($post->ID);
    foreach($categories as $category) {
    $cat_title = $categories[0]->cat_name;
    }
    } ?>
    <?php echo $cat_title; ?>
    <?php wp_title(); ?></title>

    works like a charm!

    Thanks Kafkaesqui for pointing me in the right direction! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change ‘Blog Archive’ to ‘<category>’ in post titles?’ is closed to new replies.
Skip to toolbar