php - 自定义分类分页 404(附件)
问题描述
我为媒体领域创建了一个分类法,因此我可以将所有图像分配到特定类别。我的分类页面一切正常,直到我尝试转到第 2 页,此时它会抛出 404。
在过去的几天里,我尝试了大约 8,000 种不同的重写规则函数。我已经更新了大约 9,000 次永久链接。但是当我点击第二页链接时,它仍然给我一个 404。
这是我的分类功能首先:
add_action( 'init', 'create_media_hierarchical_taxonomy', 0 );
function create_media_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Media Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Media Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Media Categories' ),
'all_items' => __( 'All Media Categories' ),
'parent_item' => __( 'Parent Media Category' ),
'parent_item_colon' => __( 'Parent Media Category:' ),
'edit_item' => __( 'Edit Media Category' ),
'update_item' => __( 'Update Media Category' ),
'add_new_item' => __( 'Add New Media Category' ),
'new_item_name' => __( 'New Media Category' ),
'menu_name' => __( 'Media Categories' ),
);
register_taxonomy('mediacat','attachment', array(
'hierarchical' => true,
'exclude_from_search' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'public' => true,
'publicly_queryable' => true,
'capability_type' => 'attachment',
'has_archive' => true,
'rewrite' => array( 'slug' => 'imagetags' ),
'update_count_callback' => '_update_post_cat_count',
'show_in_rest' => true
));
}
这是我的整个分类-mediacat php 文件
get_header();
<div id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<div class="content container-fluid" style="padding: 0 !important;">
<div class="row justify-content-center">
<div class="col-md-9">
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$current_term_id = $term->term_id;
$current_name = $term->name;
$current_taxonomy = $term->taxonomy;
$current_parent = $term->parent;
echo '<strong>'.$current_name.'</strong>';
$sub_terms = get_terms( array(
'taxonomy' => $current_taxonomy,
'child_of' => $current_term_id,
'hide_empty' => false,
) );
if ($sub_terms) {
?>
<ul class="sub-terms">
<?php foreach ($sub_terms as $sub_term) { ?>
<li><a href="<?php echo get_term_link( $sub_term ) ?>"><?php echo $sub_term->name?></a></li>
<?php }// END foreach ?>
</ul><!-- END .sub-terms -->
<?php }// END if ?>
<main>
<div class="container-fluid">
<div class="row justify-content-center">
<div class="pagination_links" style="clear: both;">
<?php $args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term_id
)
)
'orderby' => 'title',
'order' => 'asc',
'posts_per_page' => '20',
'paged' => $paged,
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
?>
Page: <?php $big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'ansel' ); // Supply translatable string
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
?>
</div>
</div>
<div class="row justify-content-center">
<form class="wpsp-page-nav-form" action="<?php echo $_SERVER['REQUEST_URI'];?>" method="get" style="text-align: center;">
<label for="sortby" class="wpsp-label wpsp-hidden"><?php _e('Jump to', 'wp-smart-pagination'); ?></label>
<div class="input-append" style="display: inline-block;">
<input class="wpsp-input-number" type="text" placeholder="page..." size="15" name="paged" />
<button class="btn" type="submit"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</form>
</div>
<div class="row justify-content-center">
<div class="container-fluid">
<div class="grid">
<div class="grid-sizer"></div>
<?php if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
$i = 0;
$item = get_the_id();
$atts = wp_get_attachment_image_src($item, 'full');
$image_all = wp_prepare_attachment_for_js($item);
$attachment_page = wp_get_attachment_url( $item );
$title = wp_prepare_attachment_for_js($item);
$image_title = wp_prepare_attachment_for_js($item);
?>
<div class="grid-item">
<img src="<?php echo $atts[0]; ?>" alt="<?php echo $image_all['alt']; ?>">
<h3 class="entry-title"><?php echo $image_title['title']; ?></h3>
<p>keywords: <em><?php if (empty($image_all['alt'])) { echo $image_all['caption']; } else { echo $image_all['alt']; } ?></em></p>
<div id="fileName<?php echo $i; ?>" style="display: none;"><?php echo $image_title['title']; ?></div>
<div class="btn-group">
<button class="btn btn-primary btn2 js-tooltip" id="btnCopy" rel="fileName<?php echo $i; ?>" type="button" data-toggle="popover" title="copied">Copy Filename</button>
<button type="button" class="btn btn-default btn2 js-tooltip" id="btnCopy" rel="fileName" type="button" data-toggle="popover" title="copied">
<!-- icon from google's material design library -->
<svg class="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" viewBox="0 0 24 24">
<path d="M17,9H7V7H17M17,13H7V11H17M14,17H7V15H14M12,3A1,1 0 0,1 13,4A1,1 0 0,1 12,5A1,1 0 0,1 11,4A1,1 0 0,1 12,3M19,3H14.82C14.4,1.84 13.3,1 12,1C10.7,1 9.6,1.84 9.18,3H5A2,2 0 0,0 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5A2,2 0 0,0 19,3Z" />
</svg>
</button>
</div>
<button id="btn_link" class="btn btn-secondary btn-lg" type="button">
<a href="#" data-toggle="modal" data-target="#galleryModal<?php echo $i; ?>" data-backdrop="static" data-keyboard="false">Suggest keywords for this item</a>
</button>
</div>
<!-- Modal -->
<div class="modal fade" id="galleryModal<?php echo $i; ?>" tabindex="-1" role="dialog" aria-labelledby="galleryModalLabel<?php echo $i; ?>" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body d-flex h-100">
<div class="row justify-content-center align-self-center">
<div class="col-md-6" style="text-align: center;">
<img src="<?php echo $atts[0]; ?>" class="img-fluid img-thumbnail">
</div>
<div class="col-md-6">
<div class="row">
<h2 class="modal-title" id="galleryModalLabel<?php echo $i; ?>"><span id="copyit"><?php echo $image_title['title']; ?></span></h2>
</div>
<div class="row">
<p>keywords: <em><?php if (empty($image_all['alt'])) { echo $image_all['caption']; } else { echo $image_all['alt']; } ?></em></p>
</div>
<div class="row" id="message">
<?php echo do_shortcode( '[gravityform id="1" field_values="filename='. $image_title['title'] .'" ajax="true"]' ); ?>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php $i++; }} wp_reset_postdata(); ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
// init Masonry
var $grid = $('.grid').masonry({
itemSelector: '.grid-item',
percentPosition: true,
columnWidth: '.grid-sizer'
});
// layout Masonry after each image loads
$grid.imagesLoaded().progress( function() {
$grid.masonry();
});
});
jQuery(document).on("click", "#btnCopy", function(ev) {
var $body = document.getElementsByTagName('body')[0];
var rel = $(this).attr("rel");
var text = $("#"+rel).text();
var $tempInput = document.createElement('INPUT');
$body.appendChild($tempInput);
$tempInput.setAttribute('value', text)
$tempInput.select();
document.execCommand('copy');
$body.removeChild($tempInput);
});
jQuery(function () {
$('[data-toggle="popover"]').popover().click(function () {
setTimeout(function () {
$('[data-toggle="popover"]').popover('hide');
}, 2000);
});
});
</script>
</div>
<div class="row justify-content-center">
<div class="pagination_links" style="clear: both;">
Page: <?php $big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'ansel' ); // Supply translatable string
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
?>
</div>
</div>
<div class="row justify-content-center">
<form class="wpsp-page-nav-form" action="<?php echo $_SERVER['REQUEST_URI'];?>" method="get" style="text-align: center;">
<label for="sortby" class="wpsp-label wpsp-hidden"><?php _e('Jump to', 'wp-smart-pagination'); ?></label>
<div class="input-append" style="display: inline-block;">
<input class="wpsp-input-number" type="text" placeholder="page..." size="15" name="paged" />
<button class="btn" type="submit"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</form>
</div>
</div>
</div>
</main>
</div>
</div>
</div>
<?php get_footer(); ?>
为了理解我为什么要这样做,有几点需要注意:
这个网站是为了让我们的现场营销人员能够看到我们在我们的图片库中拥有的内容,查看海报的选择、已创建的社交媒体图片、房产照片等。因此需要分类。我想避免使用简码创建页面,只是因为随着时间的推移我们可能需要创建越来越多的类别。但是,如果我无法让分页适用于这个自定义分类,我将继续在页面上执行简码(我让它在不需要的以前版本的网站上以这种方式工作得很好类别)。
我们还希望我们的现场人员能够为图像建议其他关键字,并能够轻松复制文件名,以便在他们为我们设计师发布的任务中引用。现在,我们的前 5 个父类别通过 Admin 中的高级自定义字段选择器显示在主页上。
无论如何,这可能都是多余的信息,但我认为这可能有助于理解我为什么要这样做。
哦!我在分类法中遇到的一个不相关的附加问题是,当我向它们添加图像时,计数不会更新。我必须手动运行 SQL 查询才能更新分类中的计数。我怀疑这与这个特定的分页问题有关,但我认为这也许值得一提。
我只是真的需要解决这个分页问题。如果这可以解决,那么我就是黄金!
解决方案
我在我的代码中找不到任何问题,所以我进行了一些试验并获得了一些解决方法的帮助。结果,我的 URL 在分页时并没有我想要的那么好,但是它们可以工作,这在这种情况下是最重要的:
<?php $big = 999999999;
for ($i=1;$i<=$loop->max_num_pages;$i++) { ?>
<a href="<?php str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ) ?>?page=<?php echo $i; ?>" class="page-numbers current" aria-current="page">Page <?php echo $i; ?></a>
<?php } ?>
想我会发布我的解决方案,以防其他人需要类似的东西。
推荐阅读
- reactjs - 顺风CSS渐变问题
- linux - Bash:使用 bash 脚本抓取文本文件以查找电话号码
- javascript - 如何自动调用类中的函数
- ios - Getting an image-related exception when using a ListWheelScrollView in Flutter
- android - Android Kotlin - 在工具栏内设置徽标
- postgresql - 针对真实 postgres 数据库的 Spring Boot 集成测试
- swift - 存在方式通知何时将呈现自定义 UIView 或 removeFromSuperview()?
- dialogflow-cx - 使用 EventHandler 警告用户他们的会话即将结束
- php - Wordpress 404 在除主页外的所有页面上都找不到
- c++ - 从 arm-none-eabi 中排除标准库