繁体   English   中英

显示父类别而不是第一类别-Wordpress

[英]Show Parent Category instead of First Category - Wordpress

我继承了一个网站,目前我拥有的代码似乎在自定义帖子的发布循环中显示出第一类,这是根据我认为的字母顺序判断的。

我有这段代码可以浏览类别名称和帖子标题:

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/,并在投资组合项目上的方框中显示类别...

就我阅读的文档而言, get_the_category应该返回一个WP_Term对象数组,该对象包含一个称为parent对象的公共变量(包含parent对象的ID)。

猜猜您应该能够通过调用以父ID为参数的方法get_the_category_by_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;
}

文档:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM