admin管理员组文章数量:1122832
I'm customizing category.php to display all attachments with a specific category, but when I click on a link for a page besides the first one, I get a 404 error. To reproduce:
- Install a new Wordpress site locally.
- Install the Blank Slate theme.
- Make a child theme that includes the files below this list.
- Upload at least 11 images to the site media library and assign a category to each of them, like
sushi
for example. - Go to http://localhost/my_site_name/category/sushi.
- Click the link "← older" at the bottom of the first 10 images to see the 404 error (it goes to http://localhost/my_site_name/category/sushi/page/2).
style.css:
/**
* Theme Name: Blank Slate Child
* Template: blankslate
*/
functions.php:
<?php
// Enqueue the child theme
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'blankslate-style', get_template_directory_uri() . '/style.css' );
}
// Add categories to attachments
function add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );
category.php (modified from Blank Slate's category.php):
<?php get_header(); ?>
<header class="header">
<h1 class="entry-title" itemprop="name"><?php single_term_title(); ?></h1>
<div class="archive-meta" itemprop="description"><?php if ( '' != get_the_archive_description() ) { echo esc_html( get_the_archive_description() ); } ?></div>
</header>
<?php
$category_name = single_cat_title("", false);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'post_type' => 'attachment',
'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $category_name
)
),
'paged' => $paged,
'posts_per_page' => 10
];
$category_query = new WP_Query($args);
// Pagination fix, via Chip Bennett:
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $category_query;
if ($category_query->have_posts()) {
while($category_query->have_posts()) {
$category_query->the_post();
the_title();
the_content();
}
}
get_template_part( 'nav', 'below' );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
php get_footer();
The nav-below
template part (from the parent Blank Slate theme) is just the_posts_navigation()
with 'prev_text' and 'next_text' arguments.
Troubleshooting ideas I tried included checking that I had the right query arguments (like making sure I was using the variable paged
instead of page
), using previous_posts_link
and next_posts_link
instead of the_posts_navigation()
, and ensuring that the posts_per_page
number matched the "Blog pages show at most" number set in the site's "Reading" settings.
Any thoughts for what I'm missing here? Thanks so much in advance!
I'm customizing category.php to display all attachments with a specific category, but when I click on a link for a page besides the first one, I get a 404 error. To reproduce:
- Install a new Wordpress site locally.
- Install the Blank Slate theme.
- Make a child theme that includes the files below this list.
- Upload at least 11 images to the site media library and assign a category to each of them, like
sushi
for example. - Go to http://localhost/my_site_name/category/sushi.
- Click the link "← older" at the bottom of the first 10 images to see the 404 error (it goes to http://localhost/my_site_name/category/sushi/page/2).
style.css:
/**
* Theme Name: Blank Slate Child
* Template: blankslate
*/
functions.php:
<?php
// Enqueue the child theme
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'blankslate-style', get_template_directory_uri() . '/style.css' );
}
// Add categories to attachments
function add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );
category.php (modified from Blank Slate's category.php):
<?php get_header(); ?>
<header class="header">
<h1 class="entry-title" itemprop="name"><?php single_term_title(); ?></h1>
<div class="archive-meta" itemprop="description"><?php if ( '' != get_the_archive_description() ) { echo esc_html( get_the_archive_description() ); } ?></div>
</header>
<?php
$category_name = single_cat_title("", false);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = [
'post_type' => 'attachment',
'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $category_name
)
),
'paged' => $paged,
'posts_per_page' => 10
];
$category_query = new WP_Query($args);
// Pagination fix, via Chip Bennett: https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $category_query;
if ($category_query->have_posts()) {
while($category_query->have_posts()) {
$category_query->the_post();
the_title();
the_content();
}
}
get_template_part( 'nav', 'below' );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
php get_footer();
The nav-below
template part (from the parent Blank Slate theme) is just the_posts_navigation()
with 'prev_text' and 'next_text' arguments.
Troubleshooting ideas I tried included checking that I had the right query arguments (like making sure I was using the variable paged
instead of page
), using previous_posts_link
and next_posts_link
instead of the_posts_navigation()
, and ensuring that the posts_per_page
number matched the "Blog pages show at most" number set in the site's "Reading" settings.
Any thoughts for what I'm missing here? Thanks so much in advance!
Share Improve this question edited Sep 12, 2024 at 23:29 The-Coder-Who-Knew-Too-Little asked Sep 11, 2024 at 6:45 The-Coder-Who-Knew-Too-LittleThe-Coder-Who-Knew-Too-Little 1456 bronze badges2 Answers
Reset to default 1Definitely not the right way to modify the main query on any given template. Hook to pre_get_posts
instead and modify it there.
For anyone's future reference. Based on Chris Cox's answer, I was able to fix the issue. I removed category.php
from the child theme, and changed the child theme's functions.php
file to the following:
<?php
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'blankslate-style', get_template_directory_uri() . '/style.css' );
}
// Add categories to attachments
function add_categories_to_attachments() {
register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );
// Via Tom J Nowell and Chip Bennett:
// https://wordpress.stackexchange.com/questions/361410/how-to-use-pre-get-posts
// https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
function wpse120407_pre_get_posts( $query ) {
$category_name = single_cat_title("", false);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ( is_category() && $query->is_main_query() ) {
// Exclude category ID 5
$query->set( 'post_type', 'attachment' );
$query->set( 'post_status', 'inherit' );
$query->set( 'tax_query', array(
array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $category_name
)
) );
$query->set( 'paged', $paged );
$query->set( 'posts_per_page', 10 );
}
}
add_action( 'pre_get_posts', 'wpse120407_pre_get_posts' );
本文标签: phpCategory Attachment Pagination 404 Error on 2nd Page
版权声明:本文标题:php - Category Attachment Pagination 404 Error on 2nd Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293227a1929101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论