Site logo

[Resolved] Static custom taxonomy archive

Home Forums Support [Resolved] Static custom taxonomy archive

Home Forums Support Static custom taxonomy archive

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2464632
    George

    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.

    #2464679
    Leo
    Staff
    Customer Support

    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 🙂

    #2464716
    George
    #2465030
    David
    Staff
    Customer Support

    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 ?

    #2469674
    George

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

    Thanks again!

    #2470191
    David
    Staff
    Customer Support

    Sounds like a good idea 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.