我能在上解决这个问题https://wordpress.stackexchange.com/users/4771/milo 建议为此,我在theme customizer中创建了一个下拉菜单,从中可以选择一个虚拟静态页面作为公文包页面。我在下拉菜单中使用了以下代码:
add_action( \'customize_register\', \'th_customize_register\' );
function th_customize_register($wp_customize) {
// Add: Drop Down Pages
$wp_customize->add_setting( \'th_portfolio_page_id\', array (
\'sanitize_callback\' => \'absint\',
) );
$wp_customize->add_control(\'th_portfolio_page_id\', array(
\'label\' => esc_html__(\'Portfolio Page\', \'themeshash\'),
\'description\' => esc_html__( \'You must have to define it if you want to set your portfolio as your homepage.\', \'themeshash\' ),
\'section\' => \'title_tagline\',
\'type\' => \'dropdown-pages\',
) );
}
设置完此下拉列表后,现在我们可以添加以下代码,有条件地将上述定义的静态页面分配给
archive-{post-type}.php
if ( get_option(\'page_on_front\') == get_theme_mod(\'th_portfolio_page_id\') ) {
add_action("pre_get_posts", "th_assign_portfolio_page");
function th_assign_portfolio_page($wp_query){
//Ensure this filter isn\'t applied to the admin area
if(is_admin()) {
return;
}
if($wp_query->get(\'page_id\') == get_option(\'page_on_front\')):
$wp_query->set(\'post_type\', \'post-type-name-here\');
$wp_query->set(\'page_id\', \'\'); //Empty
//Set properties that describe the page to reflect that
//we aren\'t really displaying a static page
$wp_query->is_page = 0;
$wp_query->is_singular = 0;
$wp_query->is_post_type_archive = 1;
$wp_query->is_archive = 1;
endif;
}
}
使用此方法,现在可以选择任何虚拟静态页面作为我们的post类型归档页面,只需一次,然后我们可以像其他页面一样将该静态页面设置/取消设置为首页。