在我的WP博客上,我的目标是重写URL,如:
domain.com/blog/**name_of_a_city**/title-of-the-article
到目前为止,我所有的链接都是这样写的:
domain.com/blog/title-of-the-article/
由于我不想重写这些现有文章的url并完全更改所有现有作品,我决定创建一个名为“articles”的新自定义帖子类型。我一直在阅读,并尝试了很多方法来做到这一点,但我遇到了一些困难。
我认为最好的方法是创建分类法,因为我将在写博客时添加许多“分类法”(城市)。例如,如果我写一篇关于巴塞罗那的文章,我希望能够添加一个城市作为分类法,并通过我的url查看它。
domain.com/barcelona/this-article-title
这是我的代码:
我经历了第一阶段:
->创建自定义帖子类型“文章”
function cptui_register_my_cpts_article() {
/**
* Post Type: Articles.
*/
$labels = array(
"name" => __( "Articles", "rowling" ),
"singular_name" => __( "Article", "rowling" ),
);
$args = array(
"label" => __( "Articles", "rowling" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => "cities",
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "author" ),
"taxonomies" => array( "category", "post_tag" ),
);
register_post_type( "article", $args );
}
add_action( \'init\', \'cptui_register_my_cpts_article\' );
->
我通过一个带有2个预注册选项(city1和city2)的插件创建了一个自定义分类法class Test_Terms {
function __construct() {
register_activation_hook( __FILE__,array( $this,\'activate\' ) );
add_action( \'init\', array( $this, \'create_cpts_and_taxonomies\' ) );
}
function activate() {
$this->create_cpts_and_taxonomies();
$this->register_new_terms();
}
function create_cpts_and_taxonomies() {
$args = array(
\'hierarchical\' => true,
\'labels\' => array(
\'name\' => _x(\'Cities\', \'taxonomy general name\' ),
\'singular_name\' => _x(\'City\', \'taxonomy singular name\'),
\'search_items\' => __(\'Search city\'),
\'popular_items\' => __(\'Popular city\'),
\'all_items\' => __(\'All city\'),
\'edit_item\' => __(\'Edit city\'),
\'edit_item\' => __(\'Edit city\'),
\'update_item\' => __(\'Update city\'),
\'add_new_item\' => __(\'Add new city\'),
\'new_item_name\' => __(\'New city\'),
\'separate_items_with_commas\' => __(\'Seperate city with commas\'),
\'add_or_remove_items\' => __(\'Add or remove city\'),
\'choose_from_most_used\' => __(\'Choose from most used city\')
),
\'query_var\' => true,
\'rewrite\' => array(\'slug\' =>\'city\', \'with_front\' =>false, \'hierarchical\'=>true),
\'capabilities\' => array(
\'manage_terms\' => \'update_plugins\',
\'edit_terms\' => \'update_plugins\',
\'delete_terms\' => \'update_plugins\',
\'assign_terms\' => \'edit_posts\'
)
);
register_taxonomy( \'city\', array( \'article\' ), $args );
}
function register_new_terms() {
$this->taxonomy = \'city\';
$this->terms = array (
\'0\' => array (
\'name\' => \'city1\',
\'slug\' => \'city1\',
\'description\' => \'for city1\',
),
\'1\' => array (
\'name\' => \'city2\',
\'slug\' => \'city2\',
\'description\' => \'for city2\',
),
);
foreach ( $this->terms as $term_key=>$term) {
wp_insert_term(
$term[\'name\'],
$this->taxonomy,
array(
\'description\' => $term[\'description\'],
\'slug\' => $term[\'slug\'],
)
);
unset( $term );
}
}
}
$Test_Terms = new Test_Terms();
->
现在,我可以从管理员那里创建一篇“新文章”,并注册一个分类法,但我希望这个分类法包含在url中,并将/name\\u of\\u作为永久链接的艺术/类型,而不是“?”现在我有/blog/?article=my_new_article
我一直在努力follow this way 和this post 但我没有成功。还有一个问题是,我不想重写现有的帖子,而只想重写在新的自定义帖子类型“Articles”下创建的帖子。提前谢谢你的建议,你可以救我!