【问题标题】:Change custom post type url更改自定义帖子类型网址
【发布时间】:2015-04-02 13:25:53
【问题描述】:

我有一个名为“投资组合”的自定义帖子类型。当我发布任何帖子时,它会将 url 生成为 /portfolio/xxx。我想将其更改为“产品”。 Change custom post-type rewrite 我已经尝试过了,但它不应该工作。

【问题讨论】:

  • 您用来生成自定义帖子类型的代码是什么样的?
  • 这个自带主题

标签: wordpress custom-post-type permalinks


【解决方案1】:

请参阅URLs of Namespaced Custom Post Types Identifiers 上的文档。您可以使用的选项之一是将'rewrite' 参数添加到register_post_type()。来自文档:

当您命名自定义帖子类型标识符并且仍想使用干净的 URL 结构时,您需要设置 register_post_type() 函数的 rewrite 参数。例如:

add_action( 'init', 'create_posttype' );
function create_posttype() {
  register_post_type( 'acme_product',
    array(
      'labels' => array(
        'name' => __( 'Portfolio' ),
        'singular_name' => __( 'Portfolio' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'products'),
    )
  );
}

然后您可能需要登录管理员并重新保存您的永久链接。

【讨论】:

  • 确实工作。事实上,这是它应该工作的唯一方式。为了帮助您,我们需要查看您的代码。
  • 我已在functions.php 中发布了您的代码,但它无法正常工作。你需要明白这一点。
  • 我的代码当然不行。这不是一段插入式代码。您需要修改自己的 OWN 代码。
  • 老兄,我不是 php 深度编码方面的专家。这就是为什么我在这里发帖询问如何实现这一目标..
【解决方案2】:

使用重写属性

$args= array(
    // other settings
    'rewrite' => array( 'slug' => 'Products' ),
);
register_post_type( 'portfolio', $args);

【讨论】:

    【解决方案3】:

    我假设您自己添加了自定义帖子类型,在这种情况下很容易。

    当您注册新的帖子类型时,您可以为该帖子类型设置任何重写规则,以作为register_post_type( 'name', $args ) 函数中使用的参数的一部分。

    如果您的帖子类型在前端可用并且是公开的,那么默认情况下 WordPress 将使用自定义帖子名称作为 slug。您可以按如下方式覆盖它:

    $args = array(
        // your other arguments
        'rewrite' => array( 
            'slug' => 'products', // use this slug instead of post type name
            'with_front' => FALSE // if you have a permalink base such as /blog/ then setting this to false ensures your custom post type permalink structure will be /products/ instead of /blog/products/
        ),
    );
    
    register_post_type( 'portfolio', $args );
    

    您可以使用的其他参数记录在 http://codex.wordpress.org/Function_Reference/register_post_type

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2012-12-14
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多