[英]How to show posts from all categories on archive page on Wordpress?
我想在 archive.php 頁面上顯示所有類別的帖子。 我制作它的方式只適用於類別頁面。 例如,如果我訪問“myurl/category/test-category/”,該頁面可以正常工作,但它只顯示來自“測試類別”的帖子。 如果我訪問“myurl/category/”,它只會返回一個空白頁。
我的存檔.php:
<?php get_header(); ?>
<div class="custom-container archive-wrapper">
<?php if(have_posts() ): while (have_posts() ): the_post(); ?>
<div class="blog-post-archive">
<div class="img-archive-wrapper d-xl-flex">
<img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>" alt="Blog image" class="blog-image-archive">
<div class="blog-post-archive-infos">
<h2 class="archive-title"><?php the_title(); ?></h2>
<span class="post-date-archive"><?php echo get_the_date(); ?></span>
<p><?php the_excerpt( ) ?></p>
<a href="<?php the_permalink();?>" class="see-more-posts"> Ver mais</a>
</div>
</div>
</div>
<?php endwhile; else: endif; ?>
<div class="pagination__links">
<?php the_posts_pagination( ); ?>
</div>
</div>
<?php get_footer(); ?>
實際上,類別模板僅用於顯示已定義類別中的帖子,因此“example.com/category/news”將顯示“新聞”類別中的所有帖子,這是所需的行為。
如果您想顯示“example.com/category”上的所有帖子,您有兩種選擇:
只需將名為“類別”go 的新頁面添加到“設置”->“永久鏈接”,select“A static 頁面”作為“您剛剛創建的頁面”。 它將顯示所有類別的所有帖子;
如果您不想創建新頁面,請在主題根目錄上添加一個文件“page_category.php”,其內容為:
<?php get_header(); ?>
<div class="custom-container archive-wrapper">
<?php
$uri = array_values(array_filter(explode('/', $_SERVER['REQUEST_URI'])));
$current_page_number = is_numeric(end($uri)) ? end($uri) : (get_query_var('paged') ? get_query_var('paged') : 1);
$wp_query = new WP_Query(array( 'post_type' => 'post', 'posts_per_page' => 10, 'paged' => $current_page_number ));
?>
<?php if(have_posts()): while (have_posts()): the_post(); ?>
<div class="blog-post-archive">
<div class="img-archive-wrapper d-xl-flex">
<img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id( $post->ID ), 'medium' ); ?>" alt="Blog image" class="blog-image-archive">
<div class="blog-post-archive-infos">
<h2 class="archive-title"><?php the_title(); ?></h2>
<span class="post-date-archive"><?php echo get_the_date(); ?></span>
<p><?php the_excerpt() ?></p>
<a href="<?php the_permalink();?>" class="see-more-posts"> Ver mais</a>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); else: endif; ?>
<div class="pagination__links">
<?php
echo paginate_links( array(
'total' => $wp_query->max_num_pages,
'current' => max( 1, $current_page_number ),
) );
?>
</div>
</div>
<?php get_footer(); ?>
而且,在您的功能上。php:
add_action('template_include', function( $template ) {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
if ( preg_match('/^category(\/page(\/[0-9]+)+)?$/', $url_path) ) {
// load the file if exists
$new_template = locate_template('page_category.php');
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
});
您也可以通過Page Template來實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.