关于php:WordPress有条件的is_single

WordPress Conditional if is_single

我试图在我的single.php页面上使用条件语句。

我想做的是,如果它是"自定义帖子类型"当前产品,则使用特定的-product.php模板页面,否则(使用标准博客帖子)使用默认的single.php页面。

我认为我使用了正确的语句,但不知道事后该怎么做(要使用该模板):

1
2
3
4
5
6
7
8
if ( is_single( 'current-products' == get_post_type() ) {    
    // If the post type is"Current Products" use this template...
    // Please help me out on this bit

} elseif ( is_single() ) {
    // If not, use the standard one...
    // please help me out on this bit
}

我认为是对的...?


WordPress自动为不同的帖子类型使用不同的模板页面,如果您的帖子类型称为products,则文件应命名为single-products.php。 您可以从那里阅读更多有关法典的信息

In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,

  • single posts of a custom post type will use single-{post_type}.php
  • and their archives will use archive-{post_type}.php

其中{post_type}是register_post_type()函数的$ post_type参数。


根据WordPress文档,您可以使用is_singular()函数:参考链接

此条件标记检查是否正在显示单个帖子,以下情况之一返回true时就是这种情况:is_single(),is_page()或is_attachment()。 如果指定了$ post_types参数,该函数将另外检查查询是否针对指定的一种职位类型。

查看"自定义帖子类型"书中的帖子时为True。

is_singular('book');

这将完成您的任务。