【问题标题】:Show Parent Category instead of First Category - Wordpress显示父类别而不是第一个类别 - Wordpress
【发布时间】:2017-05-16 22:06:57
【问题描述】:

我继承了一个网站,我目前拥有的代码似乎显示了第一个类别,我认为按字母顺序判断,在自定义帖子的帖子循环中......

我有这段代码通过类别名称和帖子标题:

class SeedPost {

    public $post_id;
    public $post_type;

    function __construct($post_id) {
        $this->post_id = $post_id;
        $this->post_type = get_post_type($this->post_id);
    }

    function display($twocol = false) {
        global $post;

        $post = get_post($this->post_id);

        $cols = $twocol ? 'two' : 'three';

        setup_postdata($post);

        if($this->post_type == 'portfolio') {
            $overlay_class = 'item__overlay--portfolio';
        } else {
            $overlay_class = 'item--cat-' . SeedHelpers::first_category();
        }
        ?>
        <a href="<?php the_permalink(); ?>">
        <div class="item item--<?php echo $cols; ?>">
            <?php
            if(has_post_thumbnail()) {
                the_post_thumbnail('news-archive', array('class' => 'item--three__child'));
            }
            ?>

                <div class="item__overlay <?php echo $overlay_class; ?>">
                    <span class="item__cat-title item__cat-title--overlay"><?php echo SeedHelpers::first_category($this->post_type); ?></span>
                    <?php get_cat_name( $cat_id ) ?>
                    <h4 class="item__title"><?php the_title(); ?></h4>
                    <!--    <?php the_excerpt(); ?> -->
                    </div>
                </div>

        </div>
        </a>
        <?php
        wp_reset_postdata();
    }

你会注意到的代码是:

SeedHelpers::first_category($this->post_type)

我相信这与一个功能有关,它将显示分配给此帖子的第一个类别。

这个功能在这里:

static function first_category($post_type = 'post') {
        if($post_type == 'post') {
            $category = get_the_category();

            if($category) {
                return $category[0]->cat_name;
            }

            return false;
        } elseif($post_type == 'portfolio') {
            $category = get_the_terms(get_the_ID(), 'portfolio-category');

            if($category) {
                return $category[0]->name;
            }

            return false;
        }
    }

我的每个帖子都有一个主类别和多个子类别,我想更改代码,使其仅显示父子类别...

我已经尝试了我在网上找到的大多数东西,但我似乎无法让它正确显示...

编辑 >>>>>>>> 我在上面的代码下面也有这段代码 - 不确定这是否与它有关?

static function category_shorthand() {
        $category = get_the_terms(get_the_ID(), 'portfolio-category');

        if($category) {
            $category_id = $category[0]->term_id;
            $shorthand = get_field('shorthand', 'portfolio-category_' . $category_id);

            if($shorthand) {
                return $shorthand;
            }

            return $category[0]->name;
        }

        return false;
    }

该网站在这里:http://ideedev.co.uk/newseed/portfolio/ 并在投资组合项目的滚动框中显示类别...

【问题讨论】:

  • 试试$category[0]-&gt;category_parent
  • 谢谢@RST 我该在哪里添加?
  • $category[0]-&gt;category_parent 代替 $category[0]-&gt;name

标签: php wordpress


【解决方案1】:

据我阅读文档,get_the_category 应该返回一个 WP_Term 对象数组,其中包含一个名为 parent 的公共变量(其中包含父项的 ID)。

猜想您应该能够使用该变量来获取父类别名称,方法是调用 get_the_category_by_ID() 方法并将父 ID 作为参数。所以你会得到:

if($category) {
    $parentId = $category[0]->parent; // contains the parent category ID
    return get_the_category_by_ID($parentId); // Returns the name of the category
}

而不是

if($category) {
    return $category[0]->cat_name;
}

文档:

【讨论】:

  • 感谢@Erik - 它似乎没有做任何事情 - 我需要更换哪些 sn-ps?我一次都试过了,但还是不行——也许我错过了什么?
  • 我刚刚用我发现的另一段代码更新了帖子 - 不确定它与类别有什么关系......
  • 你可以尝试通过echo 的值来调试你的代码。或print_r($category),这样您就可以看到该变量中的内容。像echo '&lt;pre&gt;'; print_r($category); echo '&lt;/pre&gt;' 这样的东西应该会给你一个更清晰的结果。从那里您应该查看parent 变量是否包含任何值等。
  • 真的很抱歉,我不知道那是什么意思...我可以插入: echo '
    '; print_r($类别); echo '
    ' 在 PHP 标记内,但这似乎没有做更多我明白它应该做什么??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多