Wordpress自定义URL页面

时间:2016-01-22 08:33:20

标签: wordpress url

我希望能够为用户制作自定义网址。例如,example.com/johnexample.com/jane。这些不是WordPress用户或作者,这些是我从外部资源引入的用户名。我希望所有这些URL都转到一个页面,我可以在那里显示该用户的信息。我不希望网址类似于example.com/user/john,只需example.com/john

回顾:我想看看是否在dabase中注册了用户名。 如果db中有一个用户,例如'john',则url'example.com/john'应该是可访问的,但如果'john'不是db中的有效用户名,请转到404页面。

我试图实现这一点,但不明白如何使这项工作。我在functions.php文件中添加了以下内容。

function codex_custom_init() {
  $args = array(
    'public'  => true,
    'label'   => 'profile',
    'rewrite' => array( 'slug' => '/' )
  );
  register_post_type( 'profile', $args );
}
add_action( 'init', 'codex_custom_init' );

2 个答案:

答案 0 :(得分:0)

尝试创建自定义帖子(例如:my_users),然后添加重写规则@codex

'rewrite' => array( 'slug' => '/' )

一开始,网址为my_users/user_name,重写规则为/user_name

在您的主题中定义单个tempalte,在我的情况下single-my_users.php

但是你将无法使用相同的slug创建一个页面。

我希望能帮到你。

答案 1 :(得分:0)

好吧,我很清楚这个帖子太老了,但万一其他可怜的人遇到了 Travis Hoki 上面描述的类似问题,以下是 Nozifel 在上下文中所指的内容:

      <?php

/**
 * Registers the `fake_post_type` post type.
 */
function fake_post_type_init() {
    register_post_type( 'fake-post-type', array(
        'labels'                => array(
            'name'                  => __( 'Fake post types', 'YOUR-TEXTDOMAIN' ),
            'singular_name'         => __( 'Fake post type', 'YOUR-TEXTDOMAIN' ),
            'all_items'             => __( 'All Fake post types', 'YOUR-TEXTDOMAIN' ),
            'archives'              => __( 'Fake post type Archives', 'YOUR-TEXTDOMAIN' ),
            'attributes'            => __( 'Fake post type Attributes', 'YOUR-TEXTDOMAIN' ),
            'insert_into_item'      => __( 'Insert into fake post type', 'YOUR-TEXTDOMAIN' ),
            'uploaded_to_this_item' => __( 'Uploaded to this fake post type', 'YOUR-TEXTDOMAIN' ),
            'featured_image'        => _x( 'Featured Image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'set_featured_image'    => _x( 'Set featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'remove_featured_image' => _x( 'Remove featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'use_featured_image'    => _x( 'Use as featured image', 'fake-post-type', 'YOUR-TEXTDOMAIN' ),
            'filter_items_list'     => __( 'Filter fake post types list', 'YOUR-TEXTDOMAIN' ),
            'items_list_navigation' => __( 'Fake post types list navigation', 'YOUR-TEXTDOMAIN' ),
            'items_list'            => __( 'Fake post types list', 'YOUR-TEXTDOMAIN' ),
            'new_item'              => __( 'New Fake post type', 'YOUR-TEXTDOMAIN' ),
            'add_new'               => __( 'Add New', 'YOUR-TEXTDOMAIN' ),
            'add_new_item'          => __( 'Add New Fake post type', 'YOUR-TEXTDOMAIN' ),
            'edit_item'             => __( 'Edit Fake post type', 'YOUR-TEXTDOMAIN' ),
            'view_item'             => __( 'View Fake post type', 'YOUR-TEXTDOMAIN' ),
            'view_items'            => __( 'View Fake post types', 'YOUR-TEXTDOMAIN' ),
            'search_items'          => __( 'Search fake post types', 'YOUR-TEXTDOMAIN' ),
            'not_found'             => __( 'No fake post types found', 'YOUR-TEXTDOMAIN' ),
            'not_found_in_trash'    => __( 'No fake post types found in trash', 'YOUR-TEXTDOMAIN' ),
            'parent_item_colon'     => __( 'Parent Fake post type:', 'YOUR-TEXTDOMAIN' ),
            'menu_name'             => __( 'Fake post types', 'YOUR-TEXTDOMAIN' ),
        ),
        'public'                => true,
        'hierarchical'          => false,
        'show_ui'               => true,
        'show_in_nav_menus'     => true,
        'supports'              => array( 'title', 'editor' ),
        'has_archive'           => true,
        'rewrite'               => [ 'slug' => '/' ], // <-- RIGHT HERE!
        'query_var'             => true,
        'menu_position'         => null,
        'menu_icon'             => 'dashicons-admin-post',
        'show_in_rest'          => true,
        'rest_base'             => 'fake-post-type',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
    ) );

}
add_action( 'init', 'fake_post_type_init' );