Forums

if the $cat had a parent then how to get the parent's name (4 posts)

  1. bbhit
    Member
    Posted 1 year ago #

    my categorys
    term_id name parent
    1 aa 0
    2 aa1 1
    3 aa2 1
    if the $cat had a parent then how to get the parent's name
    now the head.php
    excemple cat=2
    use the title

    <title>
    <?php /* If this is a category archive */ if (is_category()) { ?>
    <?php wp_title(); ?> - <!--here how to get the parent's name--> - <?php bloginfo('name'); ?>
    <?php } ?>
    </title>

    thanks

  2. Kafkaesqui
    Moderator
    Posted 1 year ago #

    One possibility:

    http://codex.wordpress.org/Template_Tags/get_category_parents

    And here's a manual method for collecting the name of the parent category (if there is one) -- for WordPress 2.3 and above:

    <?php
    $category = $wp_query->get_queried_object();
    if ( $category->category_parent ) {
    	$cat_parent = $wpdb->get_var("SELECT name from $wpdb->terms WHERE term_id = $category->category_parent");
    }
    ?>

    For older versions of WordPress:

    <?php
    $category = $wp_query->get_queried_object();
    if ( $category->category_parent ) {
    	$cat_parent = $wpdb->get_var("SELECT cat_name from $wpdb->categories WHERE cat_ID = $category->category_parent");
    }
    ?>

    Under either bit of code, $cat_parent will hold the name of the category parent (again, if there is one).

  3. bbhit
    Member
    Posted 1 year ago #

    thanks very much

  4. LiZharD
    Member
    Posted 1 year ago #

    The code above works only if i'm in a category template. If i'm on a single template nothing happens. Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.