conditional tags - How can I check the rewrite slug of current post type listing page

Supposed I have these urls: projects-listing-cpt and musics-listing-cpt.The custom post types was setted this way:Proj

Supposed I have these urls: /projects-listing-cpt and /musics-listing-cpt.

The custom post types was setted this way:

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'
]

How can I check the rewrite slug of current post type listing page to get something like this:

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

Supposed I have these urls: http://my-domain.tld/projects-listing-cpt and http://my-domain.tld/musics-listing-cpt.

The custom post types was setted this way:

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'
]

How can I check the rewrite slug of current post type listing page to get something like this:

if( funct_to_get_this_slug('projects-listing-cpt') == 'projects-listing-cpt' ) //show specific things for this custom post type
Share Improve this question edited Oct 1, 2019 at 21:38 ottosatto asked Oct 1, 2019 at 21:23 ottosattoottosatto 1012 bronze badges 4
  • 1 So you know the post type and simply want to retrieve its rewrite slug? – Sally CJ Commented Oct 1, 2019 at 21:45
  • Yes. How can I do that? – ottosatto Commented Oct 1, 2019 at 22:44
  • 1 Whats the actual problem here? Do you need the rewrite slug, or do you just need to know which post type is being viewed? They're different problems, and the solution for the latter will not involve the rewrite slug. – Jacob Peattie Commented Oct 2, 2019 at 1:00
  • Found the solution (It's bellow). The thing is that I've no experience in wordpress development because I use to work with laravel and I'm used to approach this in a different way. Thank you both! – ottosatto Commented Oct 2, 2019 at 1:27
Add a comment  | 

1 Answer 1

Reset to default 0

Looking to the problem from a different perspective, I've reached to a different solution to it.

I've created a function to check the post type instead of checking the slug of the post type. 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;

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745113620a4611994.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信