Loop through child product categories and change names using ACF text field

Question

On our website, we are wanting to loop through all the children of a specific product category and then display all the children of said category.

So far we are able to display all the children fine, however, for SEO purposes we feel it would be helpful to be able to alter the names of the child categories for display using the ACF text field.

For instance, say we have a category called ‘Large Umbrellas’, we want to be able to edit its name to ‘Umbrellas’ and then have the edited name display on the parent category. Hope this makes sense.

Here is how far we’ve got with the code so far.

$terms = get_terms([
    'taxonomy' => get_queried_object()->taxonomy,
    'parent'   => get_queried_object_id(),    
]);
    
    
$grade = get_field('custom_cat_title', 'product_cat' . '_' . $terms);


echo '<ul class="basel-product-categories">';
foreach ( $terms as $term) {
    echo '<li class="cat-item"><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';  
}
echo '</ul>';
echo $grade;
}
0
Timothy Davis 4 months 0 Answers 10 views 0

Leave an answer