Reputation: 221
I created a taxonomy for the media area, so I could assign all my images to specific categories. All is working fine with my taxonomy page, until I try to go to page 2, at which point, it throws a 404.
I have tried about 8,000 different rewrite rules functions that I've found over the last couple of days. I have updated permalinks about 9,000 times. But it persists to give me a 404 when I click on the 2nd page link.
Here is my taxonomy function first:
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
));
}
And here's the whole of my taxonomy-mediacat php file
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(); ?>
A couple of notes in order to maybe understand why I'm doing this:
This site is for our field marketing personnel to be able to see what we have in our image library, see choices for posters, social media images that have been created, property photos, etc. Thus the need for a taxonomy. I wanted to avoid creating pages with a shortcode, only because we may need to create more and more categories over time. If, however, I'm unable to get the pagination to work for this custom taxonomy, I will go ahead and do the shortcode on pages (I had it working this way just fine on a previous version of the site that didn't require categories).
We also want our field people to be able to suggest other keywords for images, and to be able to easily copy the file name for referencing in tasks they post for us designers. Right now, our top 5 parent categories are displayed on the homepage via Advanced Custom Fields selectors in Admin.
In any case, that may all be superfluous information here, but I thought it might help to understand WHY I'm trying to do it this way.
OH! One unrelated additional issue I'm having with the taxonomy is that the count doesn't update when I've added images to them. I have to manually run a SQL query in order for the counts in the taxonomy to update. I doubt that's related to this particular pagination issue, but I thought it was perhaps worth mentioning.
I just really need to get this pagination issue fixed up though. If that can be solved, then I'm golden!
Upvotes: 0
Views: 226
Reputation: 221
I could not find any problems in my code, so I experimented around a bit and got some help with a workaround. My URLs aren't as nice as I would like on pagination as a result, but they work, and that's the most important thing in this case:
<?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 } ?>
Figured I'd post my solution in case someone else needs something similar.
Upvotes: 1