我对此感到很困惑。我为我的CPT制作了一个所有achrive的通用模板。
功能。php:
add_filter( \'template_include\', \'wpsites_cpt_archive_page_template\', 99 );
/**
* @author Brad Dalton
* @example http://wpsites.net/
* @copyright 2014 WP Sites
*/
function wpsites_cpt_archive_page_template( $template ) {
if ( is_post_type_archive(array( drzewa_formowane, pre_bonsai ) ) ) {
$new_template = locate_template( array( \'archive-cpt.php\' ) );
if ( \'\' != $new_template ) {
return $new_template ;
}
}
return $template;
}
和存档cpt中的我的循环。php:
<div class="center">
<?php
$current_post_type = get_post_type();
// Define custom query parameters
$custom_query_args = array(
\'post_type\' => $current_post_type,
\'post_status\' => \'publish\',
\'meta_key\' => \'order\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'asc\',
\'posts_per_page\'=> \'1\'
);
// Get current page and append to custom query parameters array
$custom_query_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
<?php include \'product.php\' ?>
<?php endwhile;
endif;
// Reset postdata
wp_reset_postdata(); ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages,
\'prev_next\' => true,
\'prev_text\' => __(\' « \'),
\'next_text\' => __(\' » \'),
\'type\' => \'plain\',
) ); ?>
</nav>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
除了分页,它还可以工作。当我在仪表板中使用的“posts\\u per\\u page”值小于“Blog pages show at most”时,分页显示正确的页数,但每个链接都重定向到404。所以看起来是这样的:分页工作基于仪表板中的“博客页面最多显示”,但显示的页码基于“每页帖子数”。那么bug呢?我根据这篇帖子做了这个循环:
How to fix pagination for custom loops?
SO网友:Damian
好啊所以多亏了“米洛”,我才明白应该怎么做。首先,归档中应该是一个标准循环。然后在函数中。php我只是直接为归档页面添加了一个pre\\u get\\u posts函数,如下所示:
存档cpt。php循环:
<div class="center">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php include \'product.php\' ?>
<?php endwhile ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $wp_query->max_num_pages,
\'prev_text\' => __(\' « \'),
\'next_text\' => __(\' » \'),
) );
?>
</nav>
<?php else : ?>
<p>
<?php esc_html_e( \'Sorry, no posts matched your criteria.\' ); ?>
</p>
<?php endif; ?>
</div>
和在函数中。php:
function archive($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_archive) {
$query->set(\'post_status\', \'publish\');
$query->set(\'meta_key\', \'order\');
$query->set(\'orderby\', \'meta_value_num\');
$query->set(\'order\', \'asc\');
}
}
}
add_action(\'pre_get_posts\',\'archive\');
关键是不要创建新的WP\\U查询,而是修改现有查询。然后分页和其他功能工作正常。在pre\\u get\\u帖子中,我们还可以使用
$query->set(\'order\', \'asc\');
现在一切正常。
以下是关于以下内容的有用链接:https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts