-2

I'm wondering how you can receive a categories parent name and link for a breadcrumb I've written.

I basically want to have the following structure

if child category
blog > parent_category

else if grandchild category
blog > grand_parent_category > parent_category
  • Have you tried or investigated something already? Can you show to us? – cybmeta May 4 '16 at 8:57
  • I tried a couple of things. First requesting the page category using get_the_category hook to call the category id. Then grabbing the category name by looking at the first result in the category look and asking for cat_name. Followed by using category_link to grab the permalink to the category. – 550 May 4 '16 at 9:01
  • $category = get_the_category(); $category_title = $category[0]->cat_name; $category_link = get_category_link( $category[0]->cat_ID ); – 550 May 4 '16 at 9:01
  • also tried using the get_category_parents() hook but I only want to return the first category for the child. Also in terms of structure it needs to go up through the category hierarchy. – 550 May 4 '16 at 9:04
  • 2
    Add all that information to the question itself and please, stop posting the same question repeatedly. You have an answer already in wordpress.stackexchange.com/questions/225528/…. If that answer doesn't help you, please say it there; don't post the same question again. – cybmeta May 4 '16 at 11:43
0

This query will help you

global $wpdb;

$cat_id = 65;

// this query will get parent relationships from term_taxonomy table and get category names from terms table
$category = $wpdb->get_row(
    "SELECT t4.term_id as parent_id, t4.name as parent_name, t5.term_id as grandparent_id, t5.name as grandparent_name FROM `{$wpdb->prefix}term_taxonomy` t1
left join `{$wpdb->prefix}term_taxonomy` t2 on t2.term_id = t1.parent
left join `{$wpdb->prefix}term_taxonomy` t3 on t3.term_id = t2.parent
left join `{$wpdb->prefix}terms` t4 on t4.term_id = t2.term_id
left join `{$wpdb->prefix}terms` t5 on t5.term_id = t3.term_id
where t1.term_id={$cat_id}"
);

if ($category->grandparent_id) {
    echo "blog > {$category->grandparent_name}  > {$category->parent_name}";
} else if ($category->parent_id) {
    echo "blog > {$category->parent_name}";
}
  • Thanks for this. But I know it can be accomplished without making a database request in order to get the terms information. – 550 May 4 '16 at 9:58
  • There is a <?php get_ancestors( 208, 'category' ); ?> which return only the ID developer.wordpress.org/reference/functions/get_ancestors – Ahed Eid May 4 '16 at 10:07
  • Thanks Ahed Eid for your responses. I've posted a solution to the grandparent / parent / child items below. Now just trying to figure out the second set of problems which arise from the solution. – 550 May 4 '16 at 10:14
-1

Ok managed to get the children and parents to loop out correctly.

$catid = get_query_var( 'cat' );

while( $catid ) {
    $cat = get_category( $catid ); // Get the object for the catid.
    $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
    // the while loop will continue whilst there is a $catid
    // when there is no longer a parent $catid will be NULL so we can assign our $catParent
    $category_parent = $cat->cat_name;
    $category_parent_link = get_category_link( $cat->cat_ID );

    echo '<li class="' . esc_attr( $root_item_class ) . '" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" aria-level="3"><a href="' . esc_url( $category_parent_link ) . '" itemprop="item"><span itemprop="name">' . $category_parent . '</span></a><meta itemprop="position" content="3" /></li>';
}

Problems I have now are I need to reverse the output of the loop as it currently loops.

child > parent > grandparent

if they exist.

Also at every stage of the the loop I need to add the correct value for schema and accessbiliity logic. With each level needing to increase by 1 depending on the number of ancestors.

For example

<meta itemprop="position" content="3" /> AND aria-level="3" by each level.

with great grand parent[3] > grand parent[4] > parent[5] etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.