簡體   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