dsbifvcxr458755493 2017-05-16 12:56
浏览 31

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

I have inherited a website and the code I currently have seems to display the first category, judged by alphabetical order I think, on the post loop of a custom post...

I have this code that's pulling through category name and the title of the post:

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();
    }

The bit of code you will notice is:

SeedHelpers::first_category($this->post_type)

This relates to a function, I believe, that will display the first of the category assigned to this post.

This function is here:

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;
        }
    }

Each of my posts have one main category and multiple child categories, I would like to alter the code so it shows the parent sub category only...

I have tried most things I have found online but I can't seem to make it display properly...

EDIT >>>>>>>> I also have this bit of code underneath the bit above - not sure if this has anything to do with it?

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;
    }

The site is here: http://ideedev.co.uk/newseed/portfolio/ and displays the category in the rollover boxed on a portfolio item...

  • 写回答

1条回答 默认 最新

  • dongyiluan1718 2017-05-16 13:30
    关注

    As far as I read the documentation, get_the_category should return an Array of WP_Term objects, which contain a public variable called parent (which contain the ID of the parent).

    Guess you should be able to use that variable to get the parent category name, by calling the method get_the_category_by_ID() with the parent ID as parameter. So you'll get:

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

    instead of

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

    Docs:

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)