funkcje — Lepszy kod dla list hierarchii taksonomii?
![funkcje — Lepszy kod dla list hierarchii taksonomii?](https://oen.pl/wp-content/uploads/2023/09/apple-touch-icon@2.png)
Mam poniższy kod, aby wyświetlić taksonomie w porządku hierarchicznym, co działa dobrze. Mam witrynę z niestandardowymi polami na obrazy również na ten temat. żeby podkreślić, działa bez problemów i jest wywoływany dla dowolnego szablonu strony bez żadnych problemów.
Jednak na moje oko jest to dość nieefektywne. czy istnieje sposób na zakodowanie tego za pomocą PHP zorientowanego obiektowo, aby było trochę mniej bałaganu, wszystkie te zagnieżdżone pętle foreach wydają się po prostu… trochę prymitywne.
Dzięki
//function for listing terms including children and grandchildren terms
/*template use, e.g. for taxonomy issues and campaigns:
$selector = taxonomySelector('issues-and-campaigns');
echo $selector;
*/
fun
function taxonomySelector( $fieldName ) {
$args = array('hide_empty' => false, 'hierarchical' => true, 'parent' => 0);
$terms = get_terms($fieldName, $args);
$term_name = get_the_title();
$html="<div class="page-terms">";
$html .= '<h2>' . $term_name . '</h2></div>';
if ($fieldName == 'issues-and-campaigns') :
$image_field = 'issues_and_campaigns-taxonomy_image';
endif;
if ($fieldName == 'writing') :
$image_field = 'writing-taxonomy_image';
endif;
if ($fieldName == 'training') :
$image_field = 'training-taxonomy_image';
endif;
// echo $image_field;
//nested foreach loops to generate each level (in this case, 5 deep)
foreach ( $terms as $term ) {
$html .= '<h3><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></h3>';
$html .= '<div class="term-section">';
$image = get_field($image_field, $term);
if( $image ):
$html .= '<img src="' . $image['sizes']['medium'] . '" />';
endif;
$description = $term->description;
$html .= '<p>' . $description . '</p>';
$html .= '</div>';
$args = array(
'hide_empty' => false,
'hierarchical' => true,
'parent' => $term->term_id
);
$childterms = get_terms($fieldName, $args);
foreach ( $childterms as $childterm ) {
$html .= '<h4>' . $term->name . ' > ' . '<a href="' . get_term_link( $childterm ) . '">' . $childterm->name . '</a></h4>';
$html .= '<div class="term-section">';
$image = get_field( $image_field, $childterm);
if( $image ):
$html .= '<img src="' . $image['sizes']['medium'] . '" />';
endif;
$description = $childterm->description;
$html .= '<p>' . $description . '</p>';
$html .= '</div>';
$args = array('hide_empty' => false, 'hierarchical' => true, 'parent' => $childterm->term_id);
$grandchildterms = get_terms($fieldName, $args);
foreach ( $grandchildterms as $grandchild ) {
$html .= '<h5>' . $term->name . ' > ' . $childterm->name . ' > ' . '<a href="' . get_term_link( $grandchild ) . '">' . $grandchild->name . '</a></h5>';
$html .= '<div class="term-section">';
$image = get_field($image_field, $grandchild);
if( $image ):
$html .= '<img src="' . $image['sizes']['medium'] . '" />';
endif;
$description = $grandchild->description;
$html .= '<p>' . $description . '</p>';
$html .= '</div>';
$args2 = array('hide_empty' => false, 'hierarchical' => true, 'parent' => $grandchild->term_id);
$greatgrandchildterms = get_terms($fieldName, $args2);
foreach ( $greatgrandchildterms as $greatgrandchild ) {
$html .= '<h6>' . $term->name . ' > ' . $childterm->name . ' > ' . $grandchild->name . ' > ' . '<a href="' . get_term_link( $greatgrandchild ) . '">' . $greatgrandchild->name . '</a></h6>';
$html .= '<div class="term-section">';
$image = get_field($image_field, $greatgrandchild);
if( $image ):
$html .= '<img src="' . $image['sizes']['medium'] . '" />';
endif;
$description = $greatgrandchild->description;
$html .= '<p>' . $description . '</p>';
$html .= '</div>';
$args3 = array('hide_empty' => false, 'hierarchical' => true, 'parent' => $greatgrandchild->term_id);
$greatgreatgrandchildterms = get_terms($fieldName, $args3);
foreach ( $greatgreatgrandchildterms as $greatgreatgrandchild ) {
$html .= '<p>' . $term->name . ' > ' . $childterm->name . ' > ' . $grandchild->name . ' > ' . $greatgrandchild->name . ' > ' . '<a href="' . get_term_link( $greatgreatgrandchild ) . '">' . $greatgreatgrandchild->name .'</a></p>';
$html .= '<div class="term-section">';
$image = get_field($image_field, $greatgreatgrandchild);
if( $image ):
$html .= '<img src="' . $image['sizes']['medium'] . '" />';
endif;
$description = $greatgreatgrandchild->description;
$html .= '<p>' . $description . '</p>';
$html .= '</div>';
}// END foreach
}// END foreach
}// END foreach
}// END foreach
}// END foreach
return $html;
};