WordPress不会替换自定义帖子URL的重写块

yeotifhr  于 2023-02-11  发布在  WordPress
关注(0)|答案(1)|浏览(164)

访问https://website.com/albert/books确实会显示这位作者的所有书籍。但从WordPress检索到的列出书籍的URL将是https://website.com/%author%/books/ideas-and-opinions/
我创建了一个名为books的自定义帖子类型和名为author的自定义分类,我确实通过保存它们来刷新永久链接,但问题是wordpress的get_permalink函数仍然给我重写查询,我认为它应该被替换。

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' );

这是我在函数中的重写规则
x一个一个一个一个x一个一个二个x

bttbmeg0

bttbmeg01#

您可以添加一个函数,该函数将使用post_type_link挂钩更新该帖子类型的链接,它将https://website.com/%author%/books/ideas-and-opinions/更新为https://website.com/albert/books/ideas-and-opinions/,并将作者姓名添加到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 );

相关问题