Site logo

Archived topic

Static custom taxonomy archive

5 replies · Started by George on December 16, 2022

Viewing posts 1–6 of 6

I have a custom taxonomy that belongs to a custom post type. I want to create a static page for a particular term. The URL is something like this:
[SITEURL]/prognosis-category/bet-of-the-day/

I am using this code:

add_filter('request', function( array $query_vars ) {
    if ( is_admin() ) {
        return $query_vars;
    }

    if ( isset( $query_vars['taxonomy_name'] ) ) {
        $pagename = $query_vars['taxonomy_name'];
		if ( $pagename == 'bet-of-the-day') {
			$query_vars = array( 'pagename' => "$pagename" );
		}
    }

    return $query_vars;
} );

I was wondering if this is correct as it doesn't seem to work. I have already create a static page with the bet-of-the-day slug.

Hey George,

Correct me if I'm wrong here but I don't believe this is a theme-related issue at all.

Things like the permalink structure is entirely handled by WordPress core so I'd recommend either check with their support team or post the question in a WP general forum like this:
https://wordpress.org/support/article/settings-permalinks-screen/

Thanks for your understanding :)

after this line: $pagename = $query_vars['taxonomy_name']; use var_dump to see what $pagename returns

ie. var_dump($pagename)

I assume your condition here:

if ( $pagename == 'bet-of-the-day') {

will return false as:

$pagename = $query_vars['taxonomy_name'];

You will probably need to get the current term slug:

$current_term_slug = get_queried_object()->slug;

And compare that to your page name.

eg.

add_filter('request', function( array $query_vars ) {

    if ( is_admin() ) {
        return $query_vars;
    }
    $current_term = get_queried_object()->slug; // get the term slug
    $match_term = 'bet-of-the-day';

    if ( isset( $query_vars['taxonomy_name'] ) && $match_term == $current_term ) {
        
        $pagename = $query_vars['taxonomy_name'];
        $query_vars = array( 'pagename' => "$pagename" );
		
    }

    return $query_vars;
} );

Not sure on this to be honest, never managed to get this to work for a single term before.

Alternatively, could you just build that page using a Block Element - Loop Template ?

Hey David, I will keep the code in mind, better to go with a Loop template, I reckon.

Thanks again!

Sounds like a good idea :)

This archived topic is closed to new replies.