方法一:使用 WordPress 自带的获取当前文章类型函数来判断
比如我的自定义文章类型为 gonggao,那么判断当前文章是否为自定义文章类型为 gonggao,可以在当前文章或者主循环中添加以下代码来判断:
<?php if(get_post_type()=='gonggao'){?>
//添加你想要实现的功能代码
<?php }?>
方法二:写一个文章类型判断函数(不推荐)
将下面的代码添加到当前使用的主题中的 function.php 文件中,
function is_custom_post_type() {
global $wp_query;
$post_types = get_post_types(array('public' => true,'_builtin' => false),'names','and');
foreach ($post_types as $post_type ) {
if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
return true;
} else {
return false;
}
}
}
然后就可以直接调用下面的函数:
<?php if (is_custom_post_type()) {?>
//添加你想要实现的功能代码
<?php }?>
PS:方法二 boke112 没有测试过,个人认为使用方法一会比较好。