wordpress - 查看单个帖子时显示子类别同级

发布于 2024-11-08 13:41:08 字数 1288 浏览 0 评论 0原文

感谢大家提供了一个很棒的网站。我学到了很多。我试图让我的子类别兄弟姐妹在单击单个帖子时显示。我已经使用 WordPress 菜单设置了我的父类别。我正在使用 php 小部件在单独的菜单中调用子类别(子类别)(然后使用 CSS 进行样式设置)。我使用的代码在单击每个类别时显示特定的(相关的)子项;但是我无法弄清楚如何让它们在查看帖子时出现。

    <?php

如果(is_category()){

$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );

if(empty($subcategories) && $thiscat->parent != 0) {
    $subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}

$items='';
if(!empty($subcategories)) {
    foreach($subcategories as $subcat) {
        if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
        $items .= '
        <li class="cat-item cat-item-'.$subcat->term_id.$current.'">
            <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.'</a>
        </li>';
    }
    echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);

} ?>

我试图模仿这个菜单 在先锋 Woman.com

任何帮助或更好的解决方案将不胜感激。 谢谢,

Thank you all for a great site. I'm learning a lot. I'm trying to get my subcategory siblings to show when a single post is clicked on. I have set up my parent categories using the wordpress menu. I am using a php widget to call for the children (subcategories) in a separate menu (and then style with CSS). The code I'm using is showing the specific (relevant) children when each category is clicked on; however I am unable to figure out how to make them appear when viewing a post.

    <?php

if(is_category()) {

$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );

if(empty($subcategories) && $thiscat->parent != 0) {
    $subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}

$items='';
if(!empty($subcategories)) {
    foreach($subcategories as $subcat) {
        if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
        $items .= '
        <li class="cat-item cat-item-'.$subcat->term_id.$current.'">
            <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.'</a>
        </li>';
    }
    echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);

}
?>

I'm attempting to mimic the behavior of this menu at pioneer woman.com

Any help or a better solution would greatly be appreciated.
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-11-15 13:41:08

这列出了当前帖子类别的所有子类别:

<?php
    echo '<ul>';
    $post_child_cat = array();
    foreach((get_the_category()) as $cat) {
        $args = array( 'child_of' => $cat->cat_ID );
        $categories = get_categories( $args );
        if( $categories ) foreach( $categories as $category ) {
            echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
            '<a title="'.$category->description.'" href="';
            echo bloginfo('url');
            echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
            $category->name.'</a></li>'; 
        }
    }
   echo '</ul>';
?>

This lists all child categories of the current post's category:

<?php
    echo '<ul>';
    $post_child_cat = array();
    foreach((get_the_category()) as $cat) {
        $args = array( 'child_of' => $cat->cat_ID );
        $categories = get_categories( $args );
        if( $categories ) foreach( $categories as $category ) {
            echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
            '<a title="'.$category->description.'" href="';
            echo bloginfo('url');
            echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
            $category->name.'</a></li>'; 
        }
    }
   echo '</ul>';
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文