首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WordPress无法在single.php页面上显示自定义帖子类型

WordPress无法在single.php页面上显示自定义帖子类型
EN

Stack Overflow用户
提问于 2014-12-13 12:17:26
回答 1查看 394关注 0票数 0

到目前为止,一切都很好。我甚至可以在首页看到新的“产品”帖子。

但是对于WordPress普通的post类型,它不会像那样发生,而对于自定义的post类型,它只会像那样发生。

请帮帮忙。下面是我的代码:

代码语言:javascript
运行
复制
<?php
add_action('init', 'product_register');
function product_register() {
	$labels = array(
		'name'               => _x( 'Products', 'post type general name', 'your-plugin-textdomain' ),
		'singular_name'      => _x( 'Product', 'post type singular name', 'your-plugin-textdomain' ),
		'menu_name'          => _x( 'Products', 'admin menu', 'your-plugin-textdomain' ),
		'name_admin_bar'     => _x( 'Product', 'add new on admin bar', 'your-plugin-textdomain' ),
		'add_new'            => _x( 'Add New', 'product', 'your-plugin-textdomain' ),
		'add_new_item'       => __( 'Add New Product', 'your-plugin-textdomain' ),
		'new_item'           => __( 'New Product', 'your-plugin-textdomain' ),
		'edit_item'          => __( 'Edit Product', 'your-plugin-textdomain' ),
		'view_item'          => __( 'View Product', 'your-plugin-textdomain' ),
		'all_items'          => __( 'All Products', 'your-plugin-textdomain' ),
		'search_items'       => __( 'Search Products', 'your-plugin-textdomain' ),
		'parent_item_colon'  => __( 'Parent Products:', 'your-plugin-textdomain' ),
		'not_found'          => __( 'No products found.', 'your-plugin-textdomain' ),
		'not_found_in_trash' => __( 'No products found in Trash.', 'your-plugin-textdomain' )
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'product' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
	);
 
    register_post_type( 'product' , $args );
	flush_rewrite_rules();
}
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
	if ( ( is_home() && $query->is_main_query() ) || is_feed() )
	$query->set( 'post_type', array( 'post', 'product' ) );

	return $query;
}
?>

我的代码主要基于this link的帮助

EN

回答 1

Stack Overflow用户

发布于 2014-12-13 15:25:59

谢谢大家。现在,我的自定义帖子类型可以工作了。

我的错误是我使用以下条件将名为products.php的自定义post类型源代码文件包含到functions.php文件中:

代码语言:javascript
运行
复制
if ( is_admin() ) {
      include_once('products.php'); 
}

然后,偶然地,我删除了条件,并简单地使用:

代码语言:javascript
运行
复制
include_once('products.php');

现在它运行得很好。感谢大家的建议和帮助。他们对我很有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27455492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档