• 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • One possibility:

    https://codex.www.ads-software.com/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).

    Thread Starter bbhit

    (@bbhit)

    thanks very much

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘if the $cat had a parent then how to get the parent’s name’ is closed to new replies.