问题是如何在WordPress中创建像节点Express的路由。
/gallery/{:id or:name},访问画廊页面
我当前有项目,画廊页面(它们都使用模板)
/**
* Template Name: Gallery page
*
* @package WordPress
* @subpackage A Theme
* @since A Theme 1.0
*/
创建了一个包含图像和附加到项目的自定义画廊类型。
用
替换主题名称add_action( 'init', 'gallery_post_type' );
function gallery_post_type() {
register_post_type( 'gallery',
array(
'labels' => array(
'name' => __('Gallery Page - gallery', 'A'),
'singular_name' => __('Gallery', 'A'),
'add_new_item' => __('Add Gallery', 'A'),
'edit_item' => __('Edit Gallery', 'A'),
'new_item' => __('New Gallery', 'A'),
'view_item' => __('View Gallery', 'A'),
'search_items' => __('Search Gallery', 'A'),
'not_found' => __('No Gallery Found', 'A'),
'not_found_in_trash' => __('No Gallery found in Trash', 'A')
),
'description' => 'Gallery in the gallery page',
'hierarchical' => false,
'menu_icon' => 'dashicons-networking',
'menu_position' => 5,
'public' => true,
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'supports' => array('title')
));
}
用户单击项目块的链接时,我想将路由更改为画廊页面。
诸如域/画廊/项目名称/但它一直显示出来或什么都没有。查看画廊页面的唯一方法是进入域/画廊
我该怎么做才能创建画廊/项目名称?画廊页面-Permalink:http://wordpresslocal-clone.local/gallery/ {:name}&lt ;--我可以做这样的事情,例如Node Express
画廊帖子 - 永久链接:http://wordpresslocal-clone.local/gallery/gallery/test/
我尝试了此代码,如所建议的评论
add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
add_rewrite_rule(
'gallery/([a-zA-Z_0-9]+)/?$',
'index.php?pagename=gallery&project_name=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
var_dump($query_vars);
$query_vars[] = 'project_name';
return $query_vars;
}
请检查一下,这在我的尽头很好:
add_action('init', 'mjt_gallery_post_init');
function mjt_gallery_post_init() {
global $data;
register_post_type(
'gallery',
array(
'labels' => array(
'name' => 'Gallery Page',
'singular_name' => 'Gallery'
),
'public' => true,
'has_archive' => 'gallery',
'rewrite' => array('slug' => 'gallery',
'with_front' => true),
'supports' => array('title', 'editor', 'author', 'revisions', 'custom-fields' ),
'can_export' => true,
)
);
}
,也请重新保存永久链接设置。