【问题标题】:Wordpress custom post type rewrite rule matches all pagesWordpress 自定义帖子类型重写规则匹配所有页面
【发布时间】:2016-03-29 23:48:15
【问题描述】:

我创建了一个自定义帖子类型,并进行了重写,以使用祖父母关系作为 URL,如下所示:

function cpt_child(){
 $args = array(
  //code
  'rewrite' => array( 'slug' => '%grandparent%/%parent%', 'with_front' => false),
 );
 register_post_type( 'child', $args );
}
add_action( 'init', 'cpt_child' );

然后我更新永久链接:

add_filter( 'post_type_link', 'filter_the_post_type_link', 1, 2 );
function filter_the_post_type_link( $post_link, $post ) {
  switch( $post->post_type ) {
    case 'child':
            $post_link = get_bloginfo( 'url' );
            $relationship_child = p2p_type('children_to_parents')->get_adjacent_items($post->ID);
            $parent = $relationship['parent']->post_name;
            $relationship_parent = p2p_type('parents_to_grandparents')->get_adjacent_items($parent['parent']->ID);
            $grandparent = $relationship_parent['parent']->post_name;
            $post_link .= "/{$grandparent}/";
            $post_link .= "{$parent}/";
            $post_link .= "{$post->post_name}";
    break;
  }
  return $post_link;
}

这一切都很好,但不幸的是,重写规则也匹配常规页面,这使得它们成为 404。

我可以通过添加自定义 slug 来防止这种情况,例如“relationship”:http://example.com/relationship/grandparent/parent/child

但我真的很想使用http://example.com/grandparent/parent/child 并且它不会破坏常规页面。

我可能不得不求助于使用 htaccess 的非原生 Wordpress 重写。

提前致谢!

【问题讨论】:

    标签: php regex wordpress .htaccess custom-post-type


    【解决方案1】:

    感谢Milo 之前对其他相关问题的回答,我设法找到了解决方案。我发现这篇特别的帖子 Remove base slug in CPT & CT, use CT in permalink 非常有帮助。

    我的祖父母关系重写的初始 CPT 保持不变:

    function cpt_child(){
     $args = array(
      //code
      'rewrite' => array( 'slug' => '%grandparent%/%parent%', 'with_front' => false),
     );
     register_post_type( 'child', $args );
    }
    add_action( 'init', 'cpt_child' );
    

    然后我更新永久链接:

    add_filter( 'post_type_link', 'filter_the_post_type_link', 1, 2 );
    function filter_the_post_type_link( $post_link, $post ) {
      switch( $post->post_type ) {
        case 'child':
                $post_link = get_bloginfo( 'url' );
                $relationship_child = p2p_type('children_to_parents')->get_adjacent_items($post->ID);
                $parent = $relationship['parent']->post_name;
                $relationship_parent = p2p_type('parents_to_grandparents')->get_adjacent_items($parent['parent']->ID);
                $grandparent = $relationship_parent['parent']->post_name;
                $post_link .= "/{$grandparent}/";
                $post_link .= "{$parent}/";
                $post_link .= "{$post->post_name}";
        break;
      }
      return $post_link;
    }
    

    根据符合我需求的请求变量挂钩请求并更改查询。添加is_admin()检查以防止请求过滤器更改CPT的后端。

    // URL rewrite pages 404 fix
    if ( ! is_admin() ) {
      function svbr_fix_requests( $request ){
        // if it's not a section request and request is not empty treat request as page or post
        if( ( ! array_key_exists( 'section' , $request ) ) && ( ! empty($request) ) ){
                $request['post_type'] = array( 'post', 'page' );
        }
    
        // return request vars
        return $request;
      }
      add_filter( 'request', 'svbr_fix_requests' );
    }
    

    因为我们更改了查询变量,所以我们必须添加模板功能。

    // Use single_template filter to properly redirect to page.php and custom page templates
    function svbr_get_template_file($single_template) {
      global $post;
    
      $page_custom_template = get_post_meta( $post->ID, '_wp_page_template', true );
    
      if ($post->post_type == 'page') {
        if($page_custom_template != 'default') {
          // We are using a child theme, so get_stylesheet_directory()
          $single_template = get_stylesheet_directory() . '/' . $page_custom_template;
        }
        else {
          $single_template = get_template_directory() . '/page.php';
        }
      }
      return $single_template;
    }
    add_filter( 'single_template', 'svbr_get_template_file' );
    

    最后但并非最不重要的一点是,我们必须将模板类添加到主体以进行样式设置。

    // Add template class to body
    add_filter( 'body_class', 'template_class_name' );
    function template_class_name( $classes ) {
      global $post;
      $page_custom_template = get_post_meta( $post->ID, '_wp_page_template', true );
      $page_custom_template = str_replace('.php','',$page_custom_template);
      $page_custom_template = 'page-template-' . $page_custom_template;
        // add 'class-name' to the $classes array
        $classes[] = $page_custom_template;
        // return the $classes array
        return $classes;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多