【问题标题】:Changing permalink to custom post type将永久链接更改为自定义帖子类型
【发布时间】:2012-02-16 01:58:51
【问题描述】:

我试图让我的永久链接变成这样:

http://example.com/projects/1234

默认情况下是这样的:

http://example.com/projects/title

我尝试将“slug”设置为“projects/%post_id%”,但它看起来像这样:

http://example.com/projects/%post_id%/title

有没有办法将 slug 设置为我的自定义 slug“/”帖子的 id?任何帮助表示赞赏。

【问题讨论】:

    标签: wordpress url custom-post-type


    【解决方案1】:

    我送给您的礼物:只需将“property”替换为您的自定义帖子类型的名称即可。进入你主题的functions.php

    add_filter('post_type_link', 'property_post_type_link', 1, 3);
    
    function property_post_type_link( $link, $post = 0 ){
        if ( $post->post_type == 'property' ){
            return home_url( 'property/' . $post->ID );
        } else {
            return $link;
        }
    }
    
    add_action( 'init', 'property_rewrites_init' );
    
    function property_rewrites_init(){
        add_rewrite_rule(
            'property/([0-9]+)?$',
            'index.php?post_type=property&p=$matches[1]',
            'top' );
    }
    

    【讨论】:

    • 感谢完美。但是,就像一个注释一样,没有第二个参数被传递给 property_post_type_link 函数。相反,我不得不在函数的开头使用全局 $post。
    【解决方案2】:

    “项目”是类别还是标签等?

    如果项目是一个类别或标签,那么下面的内容应该可以工作。

    /%category%/%post_id%/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多