【问题标题】:Adding slashes to custom Post Type slug in WP在 WP 中向自定义 Post Type slug 添加斜杠
【发布时间】:2020-02-06 00:02:00
【问题描述】:

我有一个使用 RubyPress 通过 XML-RPC 创建的自定义帖子类型。

创建帖子时,我在 post_name 中指定了一些斜线(slug 或永久链接)

但是,将这些斜杠转换为连字符:

例如:year/code/some-string-name 最终成为 year-code-some-string-name

年份和代码是动态值,所以我不能使用父页面方法,因为每个帖子都有不同的值。

【问题讨论】:

    标签: wordpress rpc permalinks slash rubypress


    【解决方案1】:

    经过一番研究,这最终对我有用。

    您必须安装一个名为 Custom Permalinks 的插件,因为 WordPress 不允许您通过代码在固定链接中添加斜杠。

    以下代码将在 MyPost 每次发布时执行。除了 Publish 还可以使用其他保留字,有兴趣可以找“Post Status Transitions”。

    add_action('publish_mypost', 'add_slashes_to_mypost_slug');
    
    function add_slashes_to_mypost_slug( $post_id ) {
        $post = get_post($post_id);
        $slug = $post->post_name;
    
        $slug_exploded = explode('-', $slug);
        $year = array_shift($slug_exploded);
        $code = array_shift($slug_exploded);
        $remainder = implode('-', $slug_exploded);
        $new_slug = $year.'/'.$code.'/'.$remainder;
    
        update_post_meta($post_id, 'custom_permalink', $new_slug);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 2014-08-29
      • 2017-08-16
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2016-02-18
      相关资源
      最近更新 更多