[英]Show Parent Category instead of First Category - 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... 该站点位于: http : //ideedev.co.uk/newseed/portfolio/,并在投资组合项目上的方框中显示类别...
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). 就我阅读的文档而言,
get_the_category
应该返回一个WP_Term对象数组,该对象包含一个称为parent
对象的公共变量(包含parent
对象的ID)。
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. 猜猜您应该能够通过调用以父ID为参数的方法
get_the_category_by_ID()
来使用该变量来获取父类别名称。 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: 文档:
get_the_category()
method : https://developer.wordpress.org/reference/functions/get_the_category/ get_the_category()
方法: https : //developer.wordpress.org/reference/functions/get_the_category/ get_the_category_by_ID()
method : https://developer.wordpress.org/reference/functions/get_the_category_by_id/ get_the_category_by_ID()
方法: https : //developer.wordpress.org/reference/functions/get_the_category_by_id/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.