WordPress如何通过永久链接将帖子类型用作页面?

时间:2019-03-13 03:28:21

标签: wordpress

问题是如何创建类似WordPress中的节点表示的路由。
/ gallery / {:id或:name}进入图库页面

我目前有项目,图库页面(它们都使用模板)

 /**
 * Template Name: Gallery page
 *
 * @package WordPress
 * @subpackage  A Theme
 * @since  A Theme 1.0
 */

创建了一个自定义图库类型,其中包含图像和附加到项目的图像。

主题名称替换为A

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

当用户单击项目块中的链接时,我想更改到图库页面的路线。

类似域/画廊/项目名称/ 但是它一直显示未找到或什么也没有。 查看图库页面的唯一方法是进入域名/画廊

我应该怎么做才能创建图库/项目名称? 图库页面-固定链接:http://wordpresslocal-clone.local/gallery/ {:name} <-我可以做类似node express这样的事情

画廊帖子-固定链接:http://wordpresslocal-clone.local/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;
  }

0 个答案:

没有答案
相关问题