自定义帖子类型的wordpress存档模板
我在WordPress 3.6.1上有一个网站,我为菜谱创建了一个自定义帖子类型,代码如下:自定义帖子类型的wordpress存档模板,wordpress,Wordpress,我在WordPress 3.6.1上有一个网站,我为菜谱创建了一个自定义帖子类型,代码如下: add_action('init', 'register_cpt_my_recipes'); function register_cpt_my_recipes() { $args = array( 'hierarchical' => false, 'supports' => array('title', 'editor', 'custom-field
add_action('init', 'register_cpt_my_recipes');
function register_cpt_my_recipes() {
$args = array(
'hierarchical' => false,
'supports' => array('title', 'editor', 'custom-fields' ,'tag' , 'excerpt', 'thumbnail'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
/* Not necessary but added as a solution */
'rewrite'=> array('slug' => 'my_recipes', 'with_front' => true),
'capability_type' => 'post'
);
register_post_type('my_recipes', $args);
/* Not necessary but added as a solution */
flush_rewrite_rules(false);
}
我创建了archive-my_recipes.php自定义模板文件,但每当我导航到mysite/my_recipes/I时,我都会从archive.php中得到代码,其中包含我的所有帖子,而不仅仅是自定义帖子类型的帖子。我尝试了taxonomy-my_recipes.php,但也没有成功
此外,single-my_recipes.php运行良好。
我的自定义帖子类型的帖子出现在主循环中
我在wordpress文档网站和
我也在这里读过类似问题的答案,但还没有有效的解决方案
还尝试了以下解决方案:
我觉得你的问题与此类似
您可能需要更新永久链接设置好的,我发现这是问题的原因
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( !is_admin() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'recipes' ) );
return $query;
}
当我删除它时,归档页面正常工作,但是现在我必须编辑其他模板,通过提供查询参数来获取自定义帖子
如果有人问你如何在模板中使用它而不是默认循环
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 100 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
</ul>
<?php endwhile; // end of the loop. ?>
-
从昨天开始,我做了很多次,包括重新启动服务器、未解决问题。雪人可以使用mysite/recipes/my_recipe访问我的自定义帖子,我可以使用mysite/recipes访问我的自定义帖子存档,但输出错误,它没有使用我的自定义存档模板。一切看起来都正常,我建议您按照上面的帖子/答案的步骤进行操作,使用slug并清除缓存。复制存档文件,并给出文件名archive-{custompost type name}.php-like-archive-register\u cpt\u my\u recipes。php@ravi你没有读这个问题,我读了,它不起作用。问题是为什么。
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'posts_per_page' => 100 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
</ul>
<?php endwhile; // end of the loop. ?>