Linking custom taxonomies
-
Hi,
I’m having an issue with custom taxonomies in that I need the entry for one custom taxonomy to be linked to the same entry in others.
I am building a film review site and have set up taxonomies for Actors, Writers and Directors but of course, some people do both. At the moment, the taxonomies are all separate, so if someone clicks eg. Ben Affleck under the actor category, they will see a list of content where he is tagged as an actor.
I’d like when a user on the front-end clicks on Ben Affleck in any category, they would see a list of everything he’s done as an actor, a writer and a director. Is it possible to link individual taxonomies across different sections like this? Or any way to have it appear that way on the front-end?
Any help is much appreciated.
-
I think it works perfectly! I’ve tried my custom taxonomies, tags, author and search results and all appear to be working as intended. Thanks again for all your help and patience, apologies that it ended up taking so long!
Awesome!
No worries about the time taken, some things are like that.Hi @bcworkz,
Apologies for dragging this up again but I’ve just stumbled upon something I hadn’t thought of before: pagination. I think this will be a fairly easy fix as I think I’ve almost got it but if you could point me in the right direction, that would be great.
I’ve got pagination working on other pages using this function…
function pagination($pages = '', $range = 4) { $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages) { $pages = 1; } } if(1 != $pages) { echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>"; } } if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>"; echo "</div>\n"; } }
And I output it on the page using…
<?php if (function_exists("pagination")) { pagination($the_query->max_num_pages); } ?>
That seems to work fine on my other page templates for custom posts and I’ve got it working for my index.php too. However, I’ve done that using a custom query instead of the standard loop that we’re using on the archive. So I’ve tried adding these lines to the arrays we built…
'posts_per_page' => 10, 'paged' => $paged
But while the posts are displaying correctly, I’m getting these errors instead of my pagination:
Notice: Undefined variable: the_query in localhost\archive.php on line 62
Notice: Trying to get property of non-object in localhost\archive.php on line 62
So ultimately I’m wondering if it’s possible to add my numbered pagination to the query we previously built?
Sure, it’s possible. Since we’re utilizing the standard WP Loop and default query, merely altering its arguments before it runs, one of the usual pagination template tags (functions called from templates that generate output) should work without doing anything special. What did your theme use on archive templates before you started mucking around with things? Have you tried that?
You might instead try what the twentysixteen theme uses, here with the i18n and a11y aspects removed:
// Previous/next page navigation. the_posts_pagination( array( 'prev_text' => 'Previous page', 'next_text' => 'Next page', ) );
As you’ve discovered, pagination with custom queries can get rather tricky. One of the significant benefits of altering the default query instead of writing a new one is that WP should handle pagination without any intervention. That’s the theory anyway, it does not always work out that way.
Other template tags that might work are listed in the Pagination Codex article. This article also lists some troubleshooting steps to follow if you are having trouble. Many of them would not apply, but a couple I saw there are worth considering. First visit the Permalink Settings admin screen and verify everything is in order. By visiting, any pending rewrites are committed. That alone solves things for many people.
Are there any other custom queries on this template that could be interfering with access to the main query? In particular is query_posts() used anywhere? I hope not, no one should be using that, there are better ways.
If none of that helps and you still have trouble, let me know and I’ll dig deeper. Tell me which pagination template tags you ended up using and in what way it’s not working. Same page loads, no posts load, missing posts, repeated posts, etc.
Ok, the good news is that the pagination you posted works perfectly on my archive page! However, I’m concerned about having different pagination on this page and the other pages that require custom queries.
At the moment, my archive.php, search.php and index.php files use the standard query
if ( have_posts() ) : while ( have_posts() ) : the_post();
and the pagination you suggested works perfectly.But I have a number of templates for my custom taxonomies and my front-page which use a variation of a custom query that looks like this, with the post type changed where necessary…
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $query_args = array( 'post_type' => array ('reviews'), 'posts_per_page' => 10, 'paged' => $paged ); $the_query = new WP_Query( $query_args ); ?> <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
With those pages, I’m using the pagination I posted yesterday, with this code which connects to a function…
<?php if (function_exists("pagination")) { pagination($the_query->max_num_pages); } ?>
That works on those custom taxonomy pages, though I don’t currently have a working solution for my front-page.php which uses a different custom query, code to offset the number of posts and a counter to style the first post differently.
The pagination you have suggested seems to be the cleanest and easiest option and works great on the archive pages I mentioned above but is there a way to apply that to my custom taxonomies and front page so it is consistent across the site?
Ummm, yes and no?? ??
The the_posts_pagination() only works because it can use the main query as a reference for what to do. Once you use a custom query, it has no idea what that is. There’s been attempts to try to get template tags like this to use data from a custom query by feeding it custom query data where it is expecting to find main query data. I’ve never seen such attempts work very well except under very specific conditions.
The fact remains that when you do custom queries, you ought to manage the pagination yourself. It’s not all that difficult and works much better than trying to fool template tags. The mechanics of determining how to get proper pagination links that can be used by the custom query to get the right posts are unique to each custom query.
But you’ve worked all of that out. The issue now is the output of one appears differently than that of another. Naturally one would want a consistent appearance despite needing to use different methods to get the proper links. You have the correct links, it’s now a matter of generating the same HTML surrounding the links so the pagination appears identical despite using completely different methods to get there. One or the other (or maybe even both) needs to change its output.
Of course it’s simple to change custom pagination code’s output, but maybe you prefer this appearance over that of the_posts_pagination(). It’s also possible to change the_posts_pagination() output. If you instead use get_the_posts_pagination() to get the pagination HTML, it is returned instead of echoed out, so other script has the opportunity to alter the HTML prior to output. This can work if the alterations are minor, though it’s even possible to extract the needed links and link text, then completely rebuild the HTML around these.
If you’re going to go that far, consider using the ‘navigation_markup_template’ filter to define the HTML before it’s assembled into full output. This allows you to change the surrounding HTNL but not the anchor link itself. Unfortunately, the anchor link is hardcoded (besides the href and anchor text of course), so the only way to change that part is after it’s returned from get_the_posts_pagination().
One way or another, there are ways to get the two very different methods to at least appear the same on the page.
Ah ok. So if it’s a case of just having them appear the same, I think the simplest option is to match up the markup for my custom pagination to the posts pagination as that layout is suitable. Thanks again for the help!
- The topic ‘Linking custom taxonomies’ is closed to new replies.