删除分层自定义帖子类型的固定链接中的基本插件

时间:2016-11-03 作者:darrylyeo

我有一个hierarchical 自定义帖子类型称为project, 注册如下:

register_post_type( \'project\',
    array(
        \'public\'            => true,
        \'hierarchical\'      => true,
        \'rewrite\' => array(
            \'with_front\' => false
        )
    )
);
当前的URL如下所示:

我希望URL如下所示:

我如何才能最有效地实现这一点,使其他URL不受干扰(例如。pages、 博客posts、 其他自定义帖子类型)?

目前存在的所有解决方案要么会导致网站上其他页面出现404,要么不能正确使用嵌套URL。

2 个回复
SO网友:jgraup

这将允许您使用不带slug的post名称。基本上,只要请求链接,就可以对其进行更改,以排除基本立柱类型。任何时候只要使用一个名称运行查询,搜索中使用的可用帖子类型都会被更改,以包括您的帖子类型。

function wpse_remove_cpt_slug( $post_link, $post, $leavename ) {

    // leave these CPT alone
    $whitelist = array (\'project\');

    if ( ! in_array( $post->post_type, $whitelist ) || \'publish\' != $post->post_status )
        return $post_link;

    if( isset($GLOBALS[\'wp_post_types\'][$post->post_type],
             $GLOBALS[\'wp_post_types\'][$post->post_type]->rewrite[\'slug\'])){
        $slug = $GLOBALS[\'wp_post_types\'][$post->post_type]->rewrite[\'slug\'];
    } else {
        $slug = $post->post_type;
    }

    // remove post slug from url
    $post_link = str_replace( \'/\' . $slug  . \'/\', \'/\', $post_link );

    return $post_link;
}
add_filter( \'post_type_link\', \'wpse_remove_cpt_slug\', 10, 3 );
add_filter( \'post_link\', \'wpse_remove_cpt_slug\', 10, 3 );

function wpse_parse_request( $query ) {

    // Only noop the main query
    if ( ! $query->is_main_query() )
        return;

    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query )
         || ! isset( $query->query[\'page\'] ) )
        return;

    // \'name\' will be set if post permalinks are just post_name, otherwise the page rule will match
    if ( ! empty( $query->query[\'name\'] ) )
        $query->set( \'post_type\', array( \'post\', \'project\', \'page\' ) );
}
add_action( \'pre_get_posts\', \'wpse_parse_request\' );

参考

post_type_link 是在通过函数get\\u post\\u permalink打印之前,应用于帖子或自定义帖子类型的永久链接URL的过滤器。

post_link 是在函数get\\u permalink返回已处理的URL之前,对帖子的permalink URL应用的筛选器。

此挂钩在创建查询变量对象之后,但在实际查询运行之前调用。这个pre_get_posts 操作允许开发人员通过引用访问$query对象(您对$query所做的任何更改都直接对原始对象进行-无需返回值)。

SO网友:Abhishek R

试试这个。它将替换段塞project 父级和子级均无404错误。

/**
 * Strip the slug out of a hierarchical custom post type
 */

if ( !class_exists( \'project_Rewrites\' ) ) :

class project_Rewrites {

    private static $instance;

    public $rules;

    private function __construct() {
        /* Don\'t do anything, needs to be initialized via instance() method */
    }

    public static function instance() {
        if ( ! isset( self::$instance ) ) {
            self::$instance = new project_Rewrites;
            self::$instance->setup();
        }
        return self::$instance;
    }

    public function setup() {
        add_action( \'init\',                array( $this, \'add_rewrites\' ),            20 );
        add_filter( \'request\',             array( $this, \'check_rewrite_conflicts\' )     );
        add_filter( \'project_rewrite_rules\', array( $this, \'strip_project_rules\' )           );
        add_filter( \'rewrite_rules_array\', array( $this, \'inject_project_rules\' )          );
    }

    public function add_rewrites() {
        add_rewrite_tag( "%project%", \'(.+?)\', "project=" );
        add_permastruct( \'project\', "%project%", array(
            \'ep_mask\' => EP_PERMALINK
        ) );
    }

    public function check_rewrite_conflicts( $qv ) {
        if ( isset( $qv[\'project\'] ) ) {
            if ( get_page_by_path( $qv[\'project\'] ) ) {
                $qv = array( \'pagename\' => $qv[\'project\'] );
            }
        }
        return $qv;
    }

    public function strip_project_rules( $rules ) {
        $this->rules = $rules;
        # We no longer need the attachment rules, so strip them out
        foreach ( $this->rules as $regex => $value ) {
            if ( strpos( $value, \'attachment\' ) )
                unset( $this->rules[ $regex ] );
        }
        return array();
    }

    public function inject_project_rules( $rules ) {
        # This is the first \'page\' rule
        $offset = array_search( \'(.?.+?)/trackback/?$\', array_keys( $rules ) );
        $page_rules = array_slice( $rules, $offset, null, true );
        $other_rules = array_slice( $rules, 0, $offset, true );
        return array_merge( $other_rules, $this->rules, $page_rules );
    }
}

project_Rewrites::instance();

endif;

相关推荐

Redirect htaccess

我需要帮助重定向我的页面。例如,我有网站https://example.com. 我需要将内容从https://example.com 到https://example.com/blog. 这不是问题,我通过更改主页URL来做到这一点。这很有效。但现在我需要添加来自旧的重定向https://example.com 到https://xmpl.com.我想用。htaccess,但这不起作用。你们能帮帮我吗?以下是我对此的一些尝试,但两者都不起作用。RewriteEngine On RewriteRu