WordPress.org

Ready to get started?Download WordPress

Forums

Change 'Blog Archive' to '<category>' in post titles? (5 posts)

  1. Skeary
    Member
    Posted 9 years ago #

    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?

  2. Kafkaesqui

    Posted 9 years ago #

    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?

  3. Skeary
    Member
    Posted 9 years ago #

    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..

  4. Kafkaesqui

    Posted 9 years ago #

    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().

  5. leisurep
    Member
    Posted 7 years ago #

    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! :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.