【问题标题】:Custom Posts with same slug Redirecting to wrong post with same slug具有相同 slug 的自定义帖子重定向到具有相同 slug 的错误帖子
【发布时间】:2017-06-28 11:05:46
【问题描述】:

我有 2 个自定义帖子类型的帖子。视频帖子和城市指南帖子 第一个帖子(视频帖子)包含 url:104.130.239.132/rick-owens/,第二个帖子(城市指南帖子)包含 url:http://104.130.239.132/city-guide/rick-owens/(城市指南是固定链接结构,它是自定义帖子类型的名称)。 因此,每当我们尝试访问第一个 url 时,问题就会出现在这里,它会显示第二个 url 的模板和内容。第二个 url 是最新发布的。我尝试通过禁用 Yoast Seo 插件来解决问题,但仍然没有任何变化,并且还完成了我的永久链接刷新仍然得到相同的结果。

为第一个 url 帖子附加 Yoast SEO sn-p: 和第二个 url 帖子 的 Yoast SEO sn-p 任何帮助将不胜感激,谢谢。

加法:

这是我的 CPT 代码。

1.视频贴:

$labels = array(
'name'                => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name'       => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name'           => __( 'Video Posts', 'roots' ),
'parent_item_colon'   => __( 'Parent Video:', 'roots' ),
'all_items'           => __( 'All Videos', 'roots' ),
'view_item'           => __( 'View Video', 'roots' ),
'add_new_item'        => __( 'Add New Video', 'roots' ),
'add_new'             => __( 'Add New', 'roots' ),
'edit_item'           => __( 'Edit Video', 'roots' ),
'update_item'         => __( 'Update Video', 'roots' ),
'search_items'        => __( 'Search Video', 'roots' ),
'not_found'           => __( 'Not found', 'roots' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'roots' ),
);
$rewrite = array(
'slug'                => 'rewrite',
'with_front'          => true,
'pages'               => true,
'feeds'               => true,
);
$args = array(
'label'               => __( 'video', 'roots' ),
'description'         => __( 'Videos Post Type', 'roots' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies'          => array( 'category', 'post_tag' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'rewrite'       => $rewrite,
'capability_type'     => 'post',
'yarpp_support'    => TRUE
);
register_post_type( 'video', $args );

2.城市指南帖:

function register_post_types(){
register_post_type( 'city-guide', [
'has_archive' => TRUE,
'hierarchical' => TRUE,
'labels' => [
'name' => 'City Guide'
],
'public' => TRUE,
'supports' => ['editor', 'page-attributes', 'revisions', 'thumbnail', 'title','custom-fields','excerpt'],
'taxonomies' => array('post_tag')
] );
add_image_size( 'ipad-city-thumb', 650, 650, TRUE );
}
add_action( 'init', __NAMESPACE__.'\register_post_types', 20 );

我的代码中有这个

remove /rewrite/ slug from custom permalinks, to allow domain/slug for all post types
public function post_link_rewrite( $post_link, $post, $leavename ){
      $post_link = str_replace( '/rewrite/', '/', $post_link );

      return $post_link;
    }

【问题讨论】:

  • 你能显示注册自定义帖子类型代码吗?
  • @AshPatel 嗨,我已经添加了我的代码。请看一下

标签: php wordpress redirect custom-post-type permalinks


【解决方案1】:

用下面的代码更新注册帖子类型代码,如果可能的话,使用相同的代码作为城市指南帖子类型

$labels = array(
'name'                => _x( 'Videos', 'Post Type General Name', 'roots' ),
'singular_name'       => _x( 'Video', 'Post Type Singular Name', 'roots' ),
'menu_name'           => __( 'Video Posts', 'roots' ),
'parent_item_colon'   => __( 'Parent Video:', 'roots' ),
'all_items'           => __( 'All Videos', 'roots' ),
'view_item'           => __( 'View Video', 'roots' ),
'add_new_item'        => __( 'Add New Video', 'roots' ),
'add_new'             => __( 'Add New', 'roots' ),
'edit_item'           => __( 'Edit Video', 'roots' ),
'update_item'         => __( 'Update Video', 'roots' ),
'search_items'        => __( 'Search Video', 'roots' ),
'not_found'           => __( 'Not found', 'roots' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'roots' ),
);

$args = array(
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments',         'trackbacks', 'revisions', 'custom-fields', ),
'taxonomies'          => array( 'category', 'post_tag' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'query_var' => true,
'rewrite'       => true,
'capability_type'     => 'post',
'yarpp_support'    => TRUE
);
register_post_type( 'video', $args );

【讨论】:

  • 嗨,你在这里做了什么改变,我在这里看不到 slug
  • 我已删除 slug 并添加 query_var=> true 和 rewrite => true
  • 查询变量考虑带有 URL 查询参数的 slug..您是否尝试过该代码?有用吗?
  • 天哪,你只要重写为 true 就可以让我省去很多头痛...
【解决方案2】:

这个问题很常见,因为两种post类型的slug名称相似,所以会显示最新的。它现在不反映到帖子类型。我假设您在永久链接结构中选择了“post-name”或 %postname%。 要解决此问题,第一种也是最简单的方法是更改​​其中一个帖子的 post slug。

另一个是通过重写WordPress的类函数来改变帖子类型的永久链接结构。为此,您必须查看帖子标题下方的链接。这是因为要找出特定自定义帖子类型的帖子类型的 slug。

我已在本地安装中实现了您提供的代码。现在我有 2 个自定义帖子类型,以及两个名称为“rick owens”的帖子,其中 slug 将是“rick-owens”。首先,显示与前面提到的相同的错误。

我通过在functions.php中添加这个函数解决了这个问题

function add_rewrite_rules_custom_post(){

global $wp_rewrite;

    $structure   = 'rewrite/%rewrite%';
    $structure1  = 'city-guide/%city-guide%';

    $wp_rewrite->add_permastruct('%rewrite%', $structure, false);
    $wp_rewrite->add_permastruct('%city-guide%', $structure1, false);
}
add_action('init', 'add_rewrite_rules_custom_post');

上面的代码对我有用并经过测试,也应该对你有用。

正如我已经提到的,您应该知道该特定帖子的编辑页面中的帖子网址。

对于视频自定义帖子类型,帖子 url 应该是这样的:

www.siteurl.com/rewrite/rick-owens

对于城市指南帖子类型,帖子网址应如下所示:

www.siteurl.com/city-guide/rick-owens

如果我是正确的,相同的链接结构也应该在您的页面中,所以我在顶部提供的代码应该可以工作。如果您与我上面提到的链接不同,请在我提供的 functions.php 代码中根据它进行更改。例如, 如果您有视频自定义帖子类型,

www.siteurl.com/video/rick-owens

然后将functions.php代码改为

$structure   = 'video/%rewrite%';

see img for video url check

如果您有城市指南自定义帖子类型,

www.siteurl.com/city_guide/rick-owens

$structure1   = 'city_guide/%rewrite%';

see img for city-guide url check

这是另一种选择,如果在我的项目中发生与您类似的冲突,我通常会这样做。如果你没有得到它,请在这篇文章中提及我。

希望这会奏效。

根据您更新的流程更新答案

请使用此代码将您的帖子类型更新为 Wordpress 核心功能

function update_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'video', 'rewrite' ) );
    }
}
add_action( 'pre_get_posts', 'update_parse_request' );

希望此代码对您有用,请删除我前面提到的代码,不要忘记更新永久链接。

谢谢

注意:一旦您将代码放入其中,请不要忘记更新您的永久链接 函数.php

【讨论】:

  • 请查看我更新的qn。对于视频自定义帖子类型,帖子网址应为www.siteurl.com/rick-owens
  • 嗨,你能回复我上面的评论吗
  • 哇。它起作用了..刚刚发生了什么?非常感谢
【解决方案3】:

在您的 Wordpress 永久链接设置中,放置自定义结构,如 /%category%/%postname%/,然后在您的第一个采访帖子中,注意 yoast seo 的齿轮选项卡?您可以将 /rick-owens/ 作为规范 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 2018-09-15
    • 2017-01-31
    • 1970-01-01
    • 2019-09-06
    • 2021-10-17
    • 2018-09-23
    • 2011-07-29
    相关资源
    最近更新 更多