WordPress没有替换自定义文章网址的重写slug

huangapple go评论56阅读模式
英文:

WordPress isn't replacing the rewrite slug for custom post URLS

问题

访问https://website.com/albert/books会显示该作者的所有书籍,但从WordPress获取的列出书籍的URL将是https://website.com/%author%/books/ideas-and-opinions/

我创建了一个名为"books"的自定义文章类型和一个名为"author"的自定义分类法,我已经保存了固定链接以刷新它们,但问题是get_permalink WordPress函数仍然返回我认为应该被替换的重写查询。

以下是您提供的代码中的重要部分:

function cptui_register_my_cpts_book() {
    // 自定义文章类型:Books
    // ...
    "rewrite" => array( "slug" => "/%author%/books", "with_front" => false ),
    // ...
}

function cptui_register_my_taxes() {
    // 自定义分类法:Authors
    // ...
    "rewrite" => array( 'slug' => 'author', 'with_front' => true, ),
    // ...
}

function add_book_rewrite_rules() {
    // 添加重写规则
    add_rewrite_rule(
        '^([^/]+)/books/?$',
        'index.php?author=$matches[1]&post_type=book',
        'top'
    );
}

function add_book_query_vars( $vars ) {
    // 添加查询变量
    $vars[] = 'author';
    return $vars;
}
// Books查询
$author = get_query_var( 'author' );
$args = array(
    'post_type'      => 'book',
    'tax_query'      => array(
        array(
            'taxonomy' => 'author',
            'field'    => 'slug',
            'terms'    => $author
        )
    ),
    'posts_per_page' => -1
);
$books = new WP_Query( $args );

请注意,代码中包含了HTML转义字符,您可以在实际使用时删除它们。

英文:

Visiting https://website.com/albert/books does display all the books by this author. but the retrieved URL from WordPress for listed books would be https://website.com/%author%/books/ideas-and-opinions/

I created a custom post type named books and custom taxonomy named author, I did flush the permalinks by saving them but the issue is the get_permalink wordpress function still gives me the rewrite query which I believe it should be replaced.

function cptui_register_my_cpts_book() {

/**
 * Post Type: Books.
 */

$labels = array(
    "name" => __( "Books", "my-theme" ),
    "singular_name" => __( "Book", "my-theme" ),
    "menu_name" => __( "Books", "my-theme" ),
    "all_items" => __( "All Books", "my-theme" ),
    "add_new_item" => __( "Add New Book", "my-theme" ),
    "edit_item" => __( "Edit Book", "my-theme" ),
    "new_item" => __( "New Book", "my-theme" ),
    "view_item" => __( "View Book", "my-theme" ),
    "view_items" => __( "View Books", "my-theme" ),
    "search_items" => __( "Search Book", "my-theme" ),
    "not_found" => __( "No books found", "my-theme" ),
);

$args = array(
    "label" => __( "Books", "my-theme" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => false,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => true,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "/%author%/books", "with_front" => false ),
    "query_var" => true,
    "menu_icon" => "/wp-content/uploads/2023/02/books.webp",
    "supports" => array( "title", "thumbnail" ),
    "taxonomies" => array( "author" ),
);

register_post_type( "book", $args );
}

add_action( 'init', 'cptui_register_my_cpts_book' );


function cptui_register_my_taxes() {

/**
 * Taxonomy: Authors.
 */

$labels = array(
    "name" => __( "Authors", "my-theme" ),
    "singular_name" => __( "Author", "my-theme" ),
);

$args = array(
    "label" => __( "Authors", "my-theme" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => false,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'author', 'with_front' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => true,
    "rest_base" => "author",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
    );
register_taxonomy( "author", array( "book" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes' );

And here is my re-write rule in functions

function add_book_rewrite_rules() {
    add_rewrite_rule(
        '^([^/]+)/books/?$',
        'index.php?author=$matches[1]&post_type=book',
        'top'
    );
}
add_action( 'init', 'add_book_rewrite_rules' );

function add_book_query_vars( $vars ) {
    $vars[] = 'author';
    return $vars;
}
add_filter( 'query_vars', 'add_book_query_vars' );


Books query 

        $author = get_query_var( 'author' );
        $args = array(
            'post_type'      => 'book',
            'tax_query'      => array(
                array(
                    'taxonomy' => 'author',
                    'field'    => 'slug',
                    'terms'    => $author
                )
            ),
            'posts_per_page' => -1
        );
        $books = new WP_Query( $args );

// Loop and display esc_url( get_permalink() ) to get the URL

答案1

得分: 1

function modify_book_permalink( $post_link, $post, $leavename ) {
if ( $post->post_type == 'book' ) {
$terms = wp_get_post_terms( $post->ID, 'author' );
$author_slug = $terms[0]->slug;
$post_link = str_replace( '%author%', $author_slug, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'modify_book_permalink', 10, 3 );

英文:

You could add a function that will update the link for that post type using post_type_link hook it will update https://website.com/%author%/books/ideas-and-opinions/ to https://website.com/albert/books/ideas-and-opinions/ adding author name to the URL.

function modify_book_permalink( $post_link, $post, $leavename ) {
    if ( $post->post_type == 'book' ) {
        $terms = wp_get_post_terms( $post->ID, 'author' );
        $author_slug = $terms[0]->slug;
        $post_link = str_replace( '%author%', $author_slug, $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'modify_book_permalink', 10, 3 );

huangapple
  • 本文由 发表于 2023年2月6日 18:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定