【问题标题】:How to change slug in wordpress?如何更改wordpress中的slug?
【发布时间】:2019-02-16 20:09:18
【问题描述】:

我的 Resort 页面有这个 URL page-resorts.php:

http://localhost/testwordpress/resorts/

点击 Resort 自定义页面下的帖子链接后,我将获得自定义页面模板 (CPT) 的 URL single-resort.php:

http://localhost/testwordpress/resort/kurumba-maldives/

如您所见,resorts 已更改为 resort,因为我不能将 resorts slug 用于 slug 帖子。

我怎样才能获得这种网址:

http://localhost/testwordpress/resorts/kurumba-maldives/

在哪里使用了resorts 字而不是resort 字?

听说过自定义slug,搜了一下,还是看不懂。

【问题讨论】:

    标签: php wordpress plugins custom-post-type slug


    【解决方案1】:

    您可以通过这种方式创建自定义帖子类型。

    function create_posttype() {
      register_post_type( 'resort',
        array(
          'labels' => array(
            'name' => __( 'Resorts' ),
            'singular_name' => __( 'Resort' )
          ),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'resorts'),
        )
      );
    }
    add_action( 'init', 'create_posttype' );
    

    'rewrite' => array('slug' => 'resorts'), 这用于在 URL 上添加连线。这样您就可以获取您的网址。

    或者您可以在 functions.php

    中覆盖当前的 slug
    add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
    function wpse247328_register_post_type_args( $args, $post_type ) {
    
        if ( 'resort' === $post_type ) {
            $args['rewrite']['slug'] = 'resorts';
        }
    
        return $args;
    }
    

    更多信息,请访问。

    register post type

    Change Custom Post Type slug

    【讨论】:

      猜你喜欢
      • 2022-12-30
      • 1970-01-01
      • 2013-02-16
      • 2020-02-15
      • 2018-11-15
      • 2018-11-20
      • 2012-09-01
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多