Ich habe folgende Hierarchie: Kategorie1 - Unterkategorie1 - Unterkategorie2 - Beitrag1 Kategorie2 - Unterkategorie1
Ich muss den Namen und die Beschreibung der Unterkategorien anzeigen, wenn ich zur Kategorie //// gehe. Jetzt benutze ich diesen Code:
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo '<div id="catlist"><ul>';
$childcategories = get_categories(array(
'orderyby' => 'name',
'hide_empty' => false,
'child_of' => $this_category->cat_ID
));
foreach($childcategories as $category) {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
echo '<p>'.$category->description.'</p>';
}
echo '</ul></div>';
}
}
?>
Jetzt habe ich eine Unterkategorieliste, wenn ich auf Kategorie und leere Seite klicke, wenn ich auf Unterkategorielink klicke, aber ich muss Beiträge anzeigen, wenn ich auf Unterkategorieliste klicke.
Ihr Code überprüft, ob untergeordnete Kategorien vorhanden sind, und zeigt sie an, ob sie vorhanden sind. In diesem Fall verarbeiten Sie jedoch nicht die Schleife, in der normalerweise die Beiträge in der aktuellen Kategorie angezeigt werden.
Sie möchten Ihren Code so erweitern, dass er die Form hat:
if ( is_category() ) {
$this_category = get_category($cat);
if ( get_category_children( $this_category->cat_ID ) != "" ) {
// display the list of child categories as you currently do
} else {
/* run the standard loop to show the posts using
whatever loop code your other templates use
*/
}
}
if (is_category()){
$category = get_queried_object();
$subcategories = get_category_children( $category->term_id );
// var_dump($subcategories);
if ( $subcategories != "" ) {
echo '<div id="catlist"><ul>';
$childcategories = get_categories(array(
'orderyby' => 'name',
'hide_empty' => false,
'child_of' => $category->term_id,
));
foreach( $childcategories as $single ) {
echo '<a href="' . get_category_link( $single->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $single->name ) . '" ' . '>' . $single->name.'</a>';
echo '<p>'.$single->description.'</p>';
}
echo '</ul></div>';
} else {
// your loop.
}
<div id="content" role="main">
<?php if (is_category()) {
$this_category = get_category($cat);
if (get_category_children($this_category->cat_ID) != "") {
echo '<div id="catlist"><ul>';
$childcategories = get_categories(array(
'orderyby' => 'name',
'hide_empty' => false,
'child_of' => $this_category->cat_ID
));
foreach($childcategories as $category) {
echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
echo '<p>'.$category->description.'</p>';
} echo '</ul></div>';
}
else{
get_template_part('loop-header');
if (have_posts()) :
get_template_part('loop-actions');
get_template_part('loop-content');
get_template_part('loop-nav');
else :
get_template_part('loop-error');
endif; }}?>
<?php
?>
</div>