我是wordpress的新手,我正在尝试为多种自定义帖子类型创建一个存档页面。
我有两种自定义帖子类型,' fair'和'展览' 我目前有两个存档页面,预先获取后期操作:
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // VERY important, targets only front end queries
&& $q->is_main_query() // VERY important, targets only main query
&& $q->is_post_type_archive( 'fair' ) // Which post type archive page to target
) {
$q->set( 'oderby', 'meta_value_num' );
$q->set( 'meta_key', '_datepicker');
$q->set('showposts', 3);
$q->set('is_post_type_archive', false);
$q->set('order', 'ASC');
// Rest of your arguments to set
}
});
两个自定义帖子类型都有相同的元框,我想在存档页面上显示两种帖子类型" fair"或者在必要时使用新的
我试图添加
$q->set('post_type', array('fair', 'exhibition'));
到函数。
当我这样做时,我有两种帖子类型,但我的自定义存档页面(" archive-fair.php")似乎没有被称为
如何通过功能分页正确执行两种帖子类型的存档页面?
感谢和抱歉我的英语,我希望这是可以理解的。
编辑:我的archive-post.php页面如下:
<?php get_header();
while ( have_posts() )
{
the_post();
$p_type = get_post_type(get_the_ID());
if ($p_type == 'fair')
$img = get_post_meta(get_the_ID(), "wp_fair_attachment", true);
else
$img = get_post_meta(get_the_ID(), "wp_exhibition_attachment", true);
?>
some html
<?php } ?>
答案 0 :(得分:0)
另一种方法是使用template_include
试试这段代码,让我知道它是否有效:
add_filter( 'template_include', function( $template ){
$your_types = array( 'fair', 'exhibition' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $your_types ) )
return $template;
return get_stylesheet_directory() . '/single-fair.php';
});