我如何检查当前帖子类型列表页面的重写插件

时间:2019-10-01 作者:hailton

假设我有这些URL:http://my-domain.tld/projects-listing-cpthttp://my-domain.tld/musics-listing-cpt.

自定义帖子类型的设置方式如下:

Projects:

Text Domain: projets
$args = [
  ...
  \'rewrite\'            => array( \'slug\' => \'projects-listing-cpt\' ),
  \'capability_type\'    => \'project\'
]

Musics:

Text Domain: musics
$args = [
  ...
  \'rewrite\'            => array( \'slug\' => \'musics-listing-cpt\' ),
  \'capability_type\'    => \'music\'
]
如何检查当前帖子类型列表页面的重写段塞以获得如下内容:

if( funct_to_get_this_slug(\'projects-listing-cpt\') == \'projects-listing-cpt\' ) //show specific things for this custom post type

1 个回复
SO网友:hailton

从不同的角度来看这个问题,我找到了不同的解决方案。

我创建了一个函数来检查post类型,而不是检查post类型的slug。Actually I was trying to assign different CSS Class to each post type

add_action( \'css_class_per_post_type\', \'assign_css_class_to_each_post_type\' ); // to be called in the place I want the class name to be displayed (I am using timber as the template engine)

function assign_css_class_to_each_post_type( $context ) {

    $post_type = get_post_type($wp_query->post->ID);
    $cssClass = \'\'; // if no post type, no class is added

    if ($post_type == \'project\') {
        $cssClass = \'projects-listing-cpt\';
    } 

    if ($post_type == \'music\') {
        $cssClass = \'musics-listing-cpt\';
    }

    echo $cssClass;

}

相关推荐