Hallo,
ich brauchte eine dynamische Category-Navigation.
Auf meiner Homepage sollen nur die Topcategoryen sichtbar sein.
Klickt der Nutzer auf eine Topcategory, sollen die Unterkategorien dieses Topcategory erscheinen. Im Content stehen dann alle Beiträge der Topcategory.
Die aktuelle Category soll zusätzlich hervorgehoben werden werden.
Auf der Seite Subcategory1.2 soll folglich Topcategory1 und Subgategory1.2 hervorgehoben werden.
[size=14]Home[/SIZE]
- Topcategory1
- Topcategory2
- Topcategory3
[size=14]Topcategory 1[/SIZE]
- Topcategory1
-- Subcategory 1.1
-- Subcategory 1.2 - Topcategory2
- Topcategory3
[size=14]Topcategory 12[/SIZE]
- Topcategory1
-- Subcategory 1.1
-- Subcategory 1.2 - Topcategory2
- Topcategory3
Ich hab schon viel versucht, weiß aber noch nicht wie ich an die gewählte Topcategory/Subcategory komme.
PHP
<?php
function debug($msg) {
echo '<script>console.info("'.$msg.'")</script>';
}
if(is_home()) {
debug("home");
} else {
//current category, doenst work correctly in subcategory if there are posts assignt to more than one subcategory.
$selectedCategory = get_the_category('include=4');
$selectedCategory = $selectedCategory[0];
debug("Top-Category: ".$selectedCategory->category_parent);
debug("Sub-Category: ".$selectedCategory->cat_ID." (".$selectedCategory->cat_name.")");
}
?>
<ol>
<?php
$categories= get_categories('hierarchical=1');
// prepare output for topcategories
$topOut = "";
foreach ($categories as $cat) {
//show only parents
if($cat->category_parent == 0) {
// prepare output for subcategories
$subOut = "";
// get childcategories
if($selectedCategory->category_parent == $cat->cat_ID) {
$topClass ="active";
$subOut .= '<ol>';
$subCats = get_categories('child_of='.$cat->cat_ID);
foreach ($subCats as $subCat) {
if($selectedCategory->cat_ID == $subCat->cat_ID){
$subClass ="active";
}else{
$subClass ="inactive";
}
$subOut .= '<li class="'.$subClass.'">';
$subOut .= $subCat->cat_name;
$subOut .= ' ('.$subCat->category_count.')';
$subOut .= '</li>';
}
$subOut .= '</ol>';
} else {
$topClass = "inactive";
}
$topOut = '<li class="'.$topClass.'">';
$topOut .= $cat->cat_name;
$topOut .= ' ('.$cat->category_count.')';
$topOut .= $subOut;
$topOut .= '</li>';
echo $topOut;
}
}
?>
</ol>
Alles anzeigen