WordPress:get_the_category 并回显指向最子/最深类别的链接

发布于 2024-10-28 09:13:33 字数 1839 浏览 1 评论 0原文

我有这个代码,位于 search.php 页面上,检索每个帖子的所有类别,并回显指向第一个类别的链接:

    $category = get_the_category(); //print_r($category);
if ($category) {
  echo '<a href="' . get_category_link( $category[0]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ';

我需要做的是使用类似的代码,但它会获取最子/最深的内容数组中的类别?

这是打印出来的数组:

[0] => stdClass Object
    (
        [term_id] => 170
        [name] => ACS Series Suspended &amp; Crane Scales - EC Approved
        [slug] => uwe-acs-series-suspended-crane-scales
        [term_group] => 0
        [term_taxonomy_id] => 170
        [taxonomy] => category
        [description] => 
        [parent] => 3
        [count] => 4
        [object_id] => 1578
        [cat_ID] => 170
        [category_count] => 4
        [category_description] => 
        [cat_name] => ACS Series Suspended &amp; Crane Scales - EC Approved
        [category_nicename] => uwe-acs-series-suspended-crane-scales
        [category_parent] => 3
    )

[1] => stdClass Object
    (
        [term_id] => 3
        [name] => Crane Scales
        [slug] => crane-scales
        [term_group] => 0
        [term_taxonomy_id] => 3
        [taxonomy] => category
        [description] => 
        [parent] => 0
        [count] => 53
        [object_id] => 1578
        [cat_ID] => 3
        [category_count] => 53
        [category_description] => 
        [cat_name] => Crane Scales
        [category_nicename] => crane-scales
        [category_parent] => 0
    )

如您所见,一个类别的父级->3,另一个类别的父级->0。如何使用上面的查询仅打印父级->3 的类别的链接?

它可能很简单,但有点超出我的理解范围。任何帮助将不胜感激!

谢谢

戴夫

I have this code which is located on the search.php page and retrieves all the categories for each post and echo's out a link to the first category:

    $category = get_the_category(); //print_r($category);
if ($category) {
  echo '<a href="' . get_category_link( $category[0]->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category[0]->name ) . '" ' . '>' . $category[0]->name.'</a> ';

What I need to do is use a similar code but that gets the child-most/deepest category in the array?

This is the array thats printed out:

[0] => stdClass Object
    (
        [term_id] => 170
        [name] => ACS Series Suspended & Crane Scales - EC Approved
        [slug] => uwe-acs-series-suspended-crane-scales
        [term_group] => 0
        [term_taxonomy_id] => 170
        [taxonomy] => category
        [description] => 
        [parent] => 3
        [count] => 4
        [object_id] => 1578
        [cat_ID] => 170
        [category_count] => 4
        [category_description] => 
        [cat_name] => ACS Series Suspended & Crane Scales - EC Approved
        [category_nicename] => uwe-acs-series-suspended-crane-scales
        [category_parent] => 3
    )

[1] => stdClass Object
    (
        [term_id] => 3
        [name] => Crane Scales
        [slug] => crane-scales
        [term_group] => 0
        [term_taxonomy_id] => 3
        [taxonomy] => category
        [description] => 
        [parent] => 0
        [count] => 53
        [object_id] => 1578
        [cat_ID] => 3
        [category_count] => 53
        [category_description] => 
        [cat_name] => Crane Scales
        [category_nicename] => crane-scales
        [category_parent] => 0
    )

As you can see, one category has parent->3 and the other has parent->0. How do I use the above query to print out the link only for a category with parent->3?

Its probably quite simple but its a bit over my head. Any help would be greatly appreciated!

Thanks

Dave

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

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

发布评论

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

评论(1

薄暮涼年 2024-11-04 09:13:33

在您的 theme/functions.php 文件中添加此函数:

function get_deep_child_category( $categories )
{
    $maxId = 0;
    $maxKey = 0;
    foreach ( $categories as $key => $value )
    {
        if ( $value->parent > $maxId )
        {
            $maxId = $value->term_id;
            $maxKey = $key;
        }
    }
    return $categories[$maxKey];
}

然后假设您在 theme/search.php 中的示例中执行了

$categories = get_the_category();
if ( $categories ) :
    $deepChild = get_deep_child_category( $categories );
    ?>
        <a href="<?php echo get_category_link( $deepChild->term_id ); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $deepChild->name ); ?>"><?php echo $deepChild->name; ?></a>
    <?php 
endif;

从我的知识来看,没有其他方法可以通过 get_the_category() 对类别进行排序,但我可能会如果是这样的话,上面的代码将不是最好的做法。

Add this function in you're theme/functions.php file :

function get_deep_child_category( $categories )
{
    $maxId = 0;
    $maxKey = 0;
    foreach ( $categories as $key => $value )
    {
        if ( $value->parent > $maxId )
        {
            $maxId = $value->term_id;
            $maxKey = $key;
        }
    }
    return $categories[$maxKey];
}

Then let's say as in you're example in theme/search.php you do

$categories = get_the_category();
if ( $categories ) :
    $deepChild = get_deep_child_category( $categories );
    ?>
        <a href="<?php echo get_category_link( $deepChild->term_id ); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $deepChild->name ); ?>"><?php echo $deepChild->name; ?></a>
    <?php 
endif;

From my knoledge there is no other way of sorting categories thru get_the_category() but i might be mistaken and the code above wouldn't be the best way of doing things if so .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文