首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用wordpress中的分类法获取文章

使用wordpress中的分类法获取文章
EN

Stack Overflow用户
提问于 2020-04-23 10:27:59
回答 1查看 41关注 0票数 1

我有一个wordpress应用程序本地设置在我的电脑。在wordpress管理,我有一个国家标签下的帖子。为了更好的理解,我会附上一张图片。

我想写一个函数来得到我的前端的国家值。为此,我写了一个这样的函数

代码语言:javascript
复制
    public function get_destinations()
    {

            $bookings = get_posts(
                array(
                    'taxonomy'=> 'country',
                    'numberposts' => -1,
                )
            );
            return $bookings;
    }

但由于某种原因,该函数返回数据库中的所有帖子。我只想知道国家的名字。

我从本地url中找到了分类法,它是

代码语言:javascript
复制
http://localhost/my_project/wp-admin/edit-tags.php?taxonomy=country

我对wordpress非常陌生,不知道如何将这些数据检索到我的前端。我在这里做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-24 04:47:34

如果要显示唯一的类别或分类名称而不是get_posts,则必须使用get_terms

检查这段代码。

代码语言:javascript
复制
// Get the taxonomy's terms
    $terms = get_terms(
        array(
            'taxonomy'   => 'country',
            'hide_empty' => false, // show category even if dont have any post.
        )
    );

    // Check if any term exists
    if ( ! empty( $terms ) && is_array( $terms ) ) {
        // Run a loop and print them all
        foreach ( $terms as $term ) { ?>
            <a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
                <?php echo $term->name; ?>
            </a><?php
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61384938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档