Wordpress如何使用帖子类型作为永久链接的页面?

问题描述 投票:1回答:1

问题是如何在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')
        ));
}

当用户单击项目块中的链接时,我想将路径更改为库页面。

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

我该怎么做才能创建gallery / project-name?图库页面 - 固定链接:http://wordpresslocal-clone.local/gallery/ {:name} < - 我可以像节点表达那样做这样的事情

画廊后 - 永久链接: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;
  }
wordpress
1个回答
0
投票

请检查一下,这在我的结束时工作正常:

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

并且请重新保存永久链接设置。

© www.soinside.com 2019 - 2024. All rights reserved.