• Hi

    I’ve spent several days googling this and searched here too with no joy. I’m using Genesis with a custom child theme based on Genesis Sample, posting here as this is a WP issue I think. My PHP skills are beginner/improver level at best.

    I’ve set up a CPT and 2 custom taxonomies for it (both categories, no tags) in a basic plugin, with a single-cpt and an archive-cpt template. The taxonomy links have a template redirect to use the archive- template. The CPTs generally work as intended, except…

    The 2 categories are displayed/linked in the entry-meta/post footer, but there’s a problem with the resulting links. The first category links to the correct archive (which is using the archive-cpt template as intended), but the second category – despite the permalink being correct e.g. website/taxonomy2/term – takes me to the blog page, using index.php as the template.

    Is this a permalink problem? I’ve flushed the permalinks many times, no difference. It is producing the right path, so probably not this.

    Is it a template problem? I can’t see how it would be as both taxonomies are essentially identical in the way they are registered, both were added at the same time, both are redirected to the same template and both work in all respects except this. The permalink for the second category is correct so the redirect is working.

    I’m confused as I’ve used a very similar setup elsewhere with no issues (different theme and slightly different implementation).

    Any help much appreciated, even just the right thing to search for would help, I’m not sure what the problem is. Happy to post code excerpts but I’m not sure what is relevant to this yet. All plugins, themes etc. are up to date.

    Many thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Ha! I like your optimism in your self assessment as being at “improver level” ??

    Do you have WP_DEBUG defined as true in wp-options.php? Any warnings may lend a clue. It sounds like there’s a problem in the template redirect code. When WP gets a template path it does not know how to deal with it defaults to index.php. Are you using the “template_include” filter to accomplish template “redirects”? If not, how?

    In “template_include” check very carefully how the passed template is evaluated and that the returned path is absolutely correct. It needs to be a server path, not a HTTP URL.

    Using this filter, the process is more accurately a rewrite than a redirect, but the difference is quite pedantic, to the point where the two terms are often interchanged. The technical difference is client side vs. server side.

    Speaking of pedantic, in custom taxonomies, is misleading to use “category” and “tag” in reference to custom taxonomy terms. Simply “term” is more accurate. I knew what you meant, so it’s all good. But for future reference, since you are an “improver”, improving your terminology would be beneficial ??

    Thread Starter Jaiji

    (@jaiji)

    Thanks for that bcworkz.

    This is the function for the template redirect (via Sridhar Katakam):

    /**
     * [WordPress] Template Redirect
     * Use archive-artist.php for Artist taxonomy archives.
     */
    add_filter( 'template_include', 'sk_template_redirect' );
    function sk_template_redirect( $template ) {
    
    	if ( is_tax( 'stage' ) || is_tax( 'day') )
    		$template = get_query_template( 'archive-artist' );
    	return $template;
    
    }

    I modified his snippet to add the second taxononomy. I’ve also tried it with just the second taxonmy and it;s still not working. I’m very confused about why one works but the other doesn’t.

    Yes my terminology is not up to speed. It is improving though ?? I need more time to study, I’ve learned PHP so far by copy/pasting, referring to codex and deduction. Hard when there’s a fixed deadline approaching. Everything else is done except this and one other thing, which I’ll post separately.

    Moderator bcworkz

    (@bcworkz)

    Oooo! A sneaky problem ?? You cannot have a taxonomy named “day” because day is a reserved query var. See WordPress Query Vars for a somewhat complete list. Using reserved query vars confuses WP, it doesn’t know which day you are asking for. Don’t feel bad, this one has bitten many of us at one time or another.

    This is one of the names where it’s recommended to “namespace” it to avoid name collisions. As in adding unique, brief prefix to all global names, such as the plugin or theme intitials. I often use “bcw” in my coding, from my username here. Taxonomy slugs become something like “bcw-day”. While this avoids collisions, it makes for lousy permalinks. While permalink bases can be altered for better looking permalinks, we still cannot use “day”.

    You only need to change the taxonomy slug in the taxonomy registration and anywhere the taxonomy is referred to by slug. All the labels and other values can remain the same. Don’t forget to flush the permalinks again after making the adjustment.

    Thread Starter Jaiji

    (@jaiji)

    Thank you so much. This has been actively messing with my days for a while now, so to speak ?? I have a few sites which use ‘day’, but none use the archives.

    A final question: I actually don’t even want to display the archives for either term. All I need is to display the title of the term on each CPT, with the link stripped out. This is another thing I’ve been trying to find for some days now. Should I open another thread?

    Thanks again for your helpful support.

    Moderator bcworkz

    (@bcworkz)

    Yeah, you can use ‘day’ as long as the site doesn’t do anything that creates a name collision. It’s difficult to know when that may be, so ideally, avoid using any reserved query vars at all.

    It’s up to you if you want to start a new topic or not. Continuing here means you’re stuck with me for the most part. Most members don’t read topics with much more than a single reply, if even that. With a new topic, more members are likely to read it and possibly contribute. OTOH you may get only me again anyway ??

    I’ll tell you what. I’ll answer here. If you don’t like my answer, start a new topic and I’ll avoid replying so you are more likely to get fresh input.

    You might need to alter the template portion responsible for the term link output, replacing it with code that simply outputs the term names. Alternately, depending on how the output is achieved, you could filter the output, stripping out the HTML so only the term names remain. While it’s kind of silly to strip out HTML that a function just added, filters can be more convenient than altering templates. The extra processing involved is usually inconsequential.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘CPT archive link takes me to blog page’ is closed to new replies.