admin管理员组

文章数量:1289634

example/wp-admin/edit-tags.php?taxonomy=fixed-tags&post_type=portfolio

Please how can I rewrite:

example/blog/fixed-tags/b2b-business-to-business

to

example/portfolio/{fixed-tag} - should throw 404 if tag does not exists

Example example/portfolio/b2b-business-to-business

and force to use a new template.

The objective is to list portfolio based on tags

example/wp-admin/edit-tags.php?taxonomy=fixed-tags&post_type=portfolio

Please how can I rewrite:

example/blog/fixed-tags/b2b-business-to-business

to

example/portfolio/{fixed-tag} - should throw 404 if tag does not exists

Example example/portfolio/b2b-business-to-business

and force to use a new template.

The objective is to list portfolio based on tags

Share Improve this question edited Jul 9, 2021 at 0:07 Emeka Mbah asked Jul 8, 2021 at 20:51 Emeka MbahEmeka Mbah 1215 bronze badges 9
  • Is /blog/ your WordPress installation directory? Then you probably can do example/blog/portfolio/b2b-business-to-business, not example/portfolio/b2b-business-to-business. I can be wrong. – Sahriar Saikat Commented Jul 8, 2021 at 23:46
  • Also, what exactly is "portfolio"? custom post type? Taxonomy? A Page? – Sahriar Saikat Commented Jul 8, 2021 at 23:47
  • site.test/wp-admin/edit.php?post_type=portfolio – Emeka Mbah Commented Jul 9, 2021 at 0:04
  • site.test/wp-admin/edit-tags.php?taxonomy=fixed-tags&post_type=portfolio – Emeka Mbah Commented Jul 9, 2021 at 0:06
  • And how exactly did you get "fixed-tags" custom taxonomy and "portfolio" custom post type? Is it from a theme? Because WordPress does not have those post types and taxonomy by default. – Sahriar Saikat Commented Jul 9, 2021 at 0:18
 |  Show 4 more comments

1 Answer 1

Reset to default 1

I ended up with this:

// Register Custom Taxonomy
function fixed_tags_taxonomy() {

    $labels = array(
        'name' => _x( 'Fixed Tags', 'Taxonomy General Name', 'text_domain' ),
        'singular_name' => _x( 'Fixed Tag', 'Taxonomy Singular Name', 'text_domain' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical'=>true,
    );

    register_taxonomy( 'fixed-tags', array( 'portfolio' ), $args );
}
add_action( 'init', __NAMESPACE__ . '\\' . 'fixed_tags_taxonomy', 2 );

// rewrite url portfolio post type
// we want something in this format -> portfolio/{fixed_tag_name}  
// remember to visit wp-admin/options-permalink.php and click "save changes"
add_action('init', function() {

    add_rewrite_tag( '%portfolio%', '([^&]+)' );
    add_rewrite_rule( '^portfolio/([^/]*)/?', 'index.php?portfolio=$matches[1]&fixed_tags=$matches[1]','top' );

    // add_rewrite_tag( '%portfolio%' );
    add_rewrite_rule( '^portfolio$', 'index.php?pagename=portfolio','top' );

}, 10, 0);

// we need to register our query variable to wordpress 
// will pass it's value back to us when we call get_query_var('fixed_tags'))
add_filter( 'query_vars', function( $query_vars )
{
    $query_vars[] = 'fixed_tags';
    return $query_vars;
});

add_filter('template_redirect', function () 
{
    global $wp_query;

    $fixed_tag = get_fixed_tag(); 

    if ($fixed_tag) {
        status_header( 200 );
        $wp_query->is_404=false;

        $wp_query->set('post_type', 'portfolio');
        $wp_query->set('tax_query', [
            'relation' => 'AND',
                array(
                'taxonomy' => 'fixed-tags',
                'field'    => 'slug',
                'terms'    => array( $fixed_tag ),
            )
        ]);       
    }
});

add_filter( 'template_include', function ( $template ) 
{
    $fixed_tag = get_fixed_tag();

    if($fixed_tag){
        return locate_template( ['page-portfolio-flex-layouts.php' ] );
    }

    return $template;
});

function get_fixed_tag()
{
    $fixed_tag = sanitize_title(get_query_var('fixed_tags'));

    // we check if term is fixed tags
    // we tell wordpress to handle as 200 is found
    $term = get_term_by('slug', $fixed_tag, 'fixed-tags'); 

    return $term ? $fixed_tag : null;
}

本文标签: url rewritingHow to rewrite taxonomy and tags URL