corcel / corcel

Use WordPress backend with Laravel or any PHP application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getMainCategoryAttribute returns first tag for posts with multiple categories

bellwood opened this issue · comments

  • Corcel Version: 2.8.0
  • Framework: Laravel v5.8.38
  • PHP Version: PHP 7.4
  • Database Driver & Version: N/A

A post with multiple categories, say, 'Uncategorized' and 'Blog' will end up populating $post->main_category with the first tag for the post, not category.

In reviewing, the getMainCategoryAttribute was not looking at the 'category' taxonomy when making its decision.

The following modification resolves this issue.

I've also opted to return the first category that isn't 'Uncategorized'.

If feedback is positive, I'm glad to make a PR.

    public function getMainCategoryAttribute()
    {
        $mainCategory = 'Uncategorized';

        if (!empty($this->terms['category'])) {
            $taxonomies = array_values($this->terms['category']);

            if (count($taxonomies) > 0) {
                if ($taxonomies[0] == $mainCategory) {
                    return $taxonomies[1];
                }
                return $taxonomies[0];
            }
        }
        return $mainCategory;
    }