大约一个小时前,我提出了一个类似的问题,但从那以后,我觉得我已经做了足够多的改变,以至于如果有人第一次看到它,那将是一个混乱的局面。因此,我提出了一个新问题,这个问题与我的问题更加明确和相关。
基本上,我有一个自定义的帖子类型podcasts
我通过创建一个名为archive-podcasts.php
. 我已将此页面制作成自定义模板,并通过将其设置为首页Settings > Reading
在仪表板中。
然而,即使这样做了,归档页面也没有显示为首页,所以我做了一些研究,发现这个宝石可以放在我的functions.php
这最终让帖子的存档出现在头版。
add_action("pre_get_posts", "custom_front_page");
function custom_front_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\', \'podcasts\');
$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;
}
棒极了,但还没完成。下面是我的archive-podcasts.php
页面如下所示:<?php
/*
Template Name: Podcasts
*/
?>
<?php get_header();?>
<section id="content">
<section id="latest-blogs">
<?php
$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
$args = array( \'posts_per_page\' =>5, \'post_type\'=> \'podcasts\', \'paged\' => $paged );
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post(); ?>
<div class="article-wrapper">
<article id="post-<?php the_ID(); ?>">
<time datetime="<?php the_time(\'c\'); ?>"><?php the_time(\'F j, Y\'); ?></time>
<?php
$custom = get_post_custom($post->ID);
$buzzsprout_code = $custom["buzzsprout_code"][0];
echo do_shortcode($buzzsprout_code);
?>
<p class="read-emails"><a href="<?php the_permalink() ?>" rel="bookmark" title="View emails and comment on this episode.">View emails and comment on this episode</a></p>
</article>
</div>
<?php endwhile; ?>
<div id="pagination">
<?php my_paginate_links(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</section>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
您会注意到我正在调用函数my_paginate_links()
. 然而,调用此函数很麻烦,因为它无法正确分页帖子的存档。当我单击页码时,它会转到正确的页面,但current
CSS类不匹配。这是什么my_paginate_links()
看起来像:
function my_paginate_links() {
global $wp_rewrite, $wp_query;
$wp_query->query_vars[\'paged\'] > 1 ? $current = $wp_query->query_vars[\'paged\'] : $current = 1;
$pagination = array(
\'base\' => @add_query_arg(\'page\',\'%#%\'),
\'format\' => \'\',
\'total\' => $wp_query->max_num_pages,
\'current\' => $current,
\'prev_text\' => __(\'« previous\'),
\'next_text\' => __(\'next »\'),
\'end_size\' => 1,
\'mid_size\' => 2,
\'show_all\' => true,
\'type\' => \'list\'
);
if ( $wp_rewrite->using_permalinks() )
$pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg( \'s\', get_pagenum_link( 1 ) ) ) . \'page/%#%/\', \'paged\' );
if ( !empty( $wp_query->query_vars[\'s\'] ) )
$pagination[\'add_args\'] = array( \'s\' => get_query_var( \'s\' ) );
echo paginate_links( $pagination );
}
有人能帮我找出分页不起作用的原因吗?还有,有没有更好或更有效的方法来实现这一切?此外,我应该注意到,分页在我的常规帖子的主查询中工作得很好。他们只是不适用于这种自定义帖子类型的帖子存档。有没有办法让分页对这两个实例都起作用?
编辑:更新分页功能
function my_paginate_links() {
global $wp_rewrite, $postslist;
$postslist->query_vars[\'page\'] > 1 ? $current = $postslist->query_vars[\'page\'] : $current = 1;
$pagination = array(
\'base\' => @add_query_arg(\'page\',\'%#%\'),
\'format\' => \'\',
\'total\' => $postslist->max_num_pages,
\'current\' => $current,
\'prev_text\' => __(\'« previous\'),
\'next_text\' => __(\'next »\'),
\'end_size\' => 1,
\'mid_size\' => 2,
\'show_all\' => true,
\'type\' => \'list\'
);
if ( $wp_rewrite->using_permalinks() )
$pagination[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg( \'s\', get_pagenum_link( 1 ) ) ) . \'page/%#%/\', \'paged\' );
if ( !empty( $postslist->query_vars[\'s\'] ) )
$pagination[\'add_args\'] = array( \'s\' => get_query_var( \'s\' ) );
echo paginate_links( $pagination );
}