最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create custom url structure based on custom post type rewrite slug, taxonomy and category slug?

programmeradmin1浏览0评论

I am working on a test. Where i want the follow to be able to work:

/referenties/ = page
/referenties/hoofdcategorie/ = main taxonomy
/referenties/hoofdcategorie/subcategorie/ = sub taxonomy
/referenties/hoofdcategorie/subcategorie/referentienaam-3/ = single reference

For so far I got this working, but I allso want the following to be able to work:
/referenties/hoofdcategorie/referentienaam/ = single reference but shows 404

Obviously this is because WP thinks this URL should be a subtax, how can we overcome this and let it check for
a single first
if it's available show it
if not, check if there is a taxonomy available and show it
if not THAN show a 404.

Can someone help me with this?

The current code is:

function cptui_register_my_cpts_referentie() {

    /**
     * Post Type: Referenties.
     */

    $labels = [
        "name" => __( "Referenties", "webtontheme" ),
        "singular_name" => __( "Referentie", "webtontheme" ),
        "menu_name" => __( "Referenties", "webtontheme" ),
        "all_items" => __( "Alle referenties", "webtontheme" ),
        "add_new" => __( "Nieuwe referentie", "webtontheme" ),
        "add_new_item" => __( "Voeg referentie toe", "webtontheme" ),
        "edit_item" => __( "Bewerk referentie", "webtontheme" ),
        "new_item" => __( "Nieuwe referentie", "webtontheme" ),
        "view_item" => __( "Bekijk referentie", "webtontheme" ),
    ];

    $args = [
        "label" => __( "Referenties", "webtontheme" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => [ "slug" => "referenties", "with_front" => true ],
        "query_var" => true,
        "supports" => [ "title", "revisions" ],
    ];

    register_post_type( "referentie", $args );
}

add_action( 'init', 'cptui_register_my_cpts_referentie' );

function cptui_register_my_taxes_referentie_category() {

    /**
     * Taxonomy: Categorieën.
     */

    $labels = [
        "name" => __( "Categorieën", "webtontheme" ),
        "singular_name" => __( "Categorie", "webtontheme" ),
    ];

    $args = [
        "label" => __( "Categorieën", "webtontheme" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'referenties', 'with_front' => true,  'hierarchical' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "referentie_category",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        ];
    register_taxonomy( "referentie_category", [ "referentie" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_referentie_category' );






function keha_add_rewrite_rules() {
    add_rewrite_rule( '^referenties/(.+?)/(.+?)/(.+?)$', 'index.php?referentie_category=$matches[1]&referentie_category=$matches[2]&referentie=$matches[3]', 'top' );
    add_rewrite_rule( '^referenties/(.+?)/(.+?)/$', 'index.php?referentie=$matches[2]', 'top' );
    add_rewrite_rule( '^referenties/(.+?)/(.+?)/(.+?)$', 'index.php?referentie=$matches[3]', 'top' );
    add_rewrite_rule( '^referenties/(.+?)/(.+?)/?$', 'index.php?referentie_category=$matches[2]', 'top' );
    add_rewrite_rule( '^referenties/(.+?)$', 'index.php?referentie_category=$matches[1]', 'top' );
}
add_action('init', 'keha_add_rewrite_rules');




function keha_post_type_link( $post_link, $post, $leavename ) {

        global $wp_rewrite;

        $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
        if ( $draft_or_pending and !$leavename ) {
                return $post_link;
        }

        if ( $post->post_type == 'referentie' ) {

                $post_type_object = get_post_type_object( $post->post_type );
                $post_link = home_url() . '/' . $post_type_object->rewrite['slug'] . '/';
                $parent_dirs = '';
                if ( $terms = get_the_terms( $post->ID, 'referentie_category' ) ) {
                        foreach ( $terms as $term ) {
                                if ( $term->parent != 0 ) {
                                        $dirs = keha_get_taxonomy_parents( $term->term_id, 'referentie_category', false, '/', true );
                                } else {
                                        $dirs = $term->slug.'/';
                                }
                        }
                }
                $post_link = $post_link . $dirs . $post->post_name . '/';
        }

        return $post_link;
}
add_filter( 'post_type_link', 'keha_post_type_link', 10, 3 );





function keha_get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
        $chain = '';
        $parent = get_term( $id, $taxonomy, OBJECT, 'raw');
        if ( is_wp_error( $parent ) ) {
                return $parent;
        }

        if ( $nicename ){
                $name = $parent->slug;
        } else {
                $name = $parent->name;
        }

        if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
                $visited[] = $parent->parent;
                $chain .= keha_get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
        }

        if ( $link ) {
                $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
        }else {
                $chain .= $name.$separator;
        }
        return $chain;
}
```

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论