带有相同条目的自定义帖子重定向到具有相同条目的错误帖子 [英] Custom Posts with same slug Redirecting to wrong post with same slug

查看:77
本文介绍了带有相同条目的自定义帖子重定向到具有相同条目的错误帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个自定义帖子类型的帖子.视频帖子和城市指南帖子 第一个帖子(视频帖子)包含以下网址: 104.130.239.132/rick-owens/帖子(City-Guide Post)别名网址: http://104.130.239.132/city-guide/rick-owens/(城市指南是固定链接,是自定义帖子类型的名称). 因此,每当我们尝试访问第一个URL时都会出现问题,它显示第二个URL的模板和内容.第二个URL是最新发布的.我试图通过禁用Yoast Seo Plugin来解决问题,但仍然没有任何变化,并且还做了我的永久链接刷新仍然得到相同的结果.

I have 2 Custom Post type posts.The video Post and City-guide Post The first Post(Video Post) contains the url: 104.130.239.132/rick-owens/ and the second Post(City-Guide Post) conatins url:http://104.130.239.132/city-guide/rick-owens/ (city-guide is the permalink structured which is the name of custom post type). SO the issue arrise here whenever we try to access the first url it displays the template and content of 2nd url.The 2nd url is latest published.I tried from myself to solve by disabling the Yoast Seo Plugin still no change and also done my permalink refreshed still got same results.

为第一个网址发布附加Yoast SEO代码段:和第二个网址帖子的Yoast SEO代码段 谢谢您的帮助.

Attaching the Yoast SEO snippet for first url post: and the Yoast SEO snippet of second url post Any Help will be appreciated,Thanks.

添加:

这是我的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.City Guide帖子:

2.City Guide Posts :

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;
    }

推荐答案

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

This issue is really common, since the slug name of the two post type is similar, the one which is latest one will be displayed. It doesn't reflect to the post type now. I assume you have chosen "post-name" or %postname% in a permalink structure. To solve the issue, the first and easiest way is to change the post slug of one of the posts.

另一种方法是通过将WordPress的重写类函数从functions.php更改帖子类型的永久链接结构.为此,您必须查看帖子标题下方的链接.这是因为要找出特定自定义帖子类型的帖子类型的信息.

Another is to change the permalink structure of the post type from the functions.php through rewrite class functions of WordPress. To do so, you have to look at the link below the post title. This is because to find out the slug of the post type of the particular custom post type.

我已经在本地安装中实现了您提供的代码.现在,我有2种自定义帖子类型,并且您的帖子名称都为"rick owens",其中"slug owens"为"rick-owens".首先,显示与上述相同的错误.

I have implemented your provided code in my local installation. Now i have 2 custom post type as of yours and the post in both names "rick owens" which slug will be "rick-owens". Firstly, the same error is displayed as of have mentioned.

我通过在functions.php中添加此功能解决了这个问题

I solved this problem by adding this function in 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');

上面的代码对我有用,并且已经过测试,也应该对您有用.

Above this code worked for me and tested and should work for you also.

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

As i have already mentioned, you should know the post url from the edit page of that particular post.

对于视频自定义帖子类型,帖子网址应如下所示:

For video custom post type, the post url should be like this:

www.siteurl.com/rewrite/rick-owens

www.siteurl.com/rewrite/rick-owens

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

For city guide post type, the post url should be like this:

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

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

如果我是对的,那么您的页面中也应该包含相同的链接结构,因此该代码应该可以正常工作(我在顶部提供了该代码).如果您有别于我上面提到的链接,请在我提供的functions.php代码中根据它进行更改.例如, 如果您有视频自定义帖子类型,则

If I am correct, the same link structure should be in your page too, so the code should work which I provided at the top. If you have different than that i have mentioned link above, then change according to it in the functions.php code that i have provided. For example, If you have for video custom post type,

www.siteurl.com/video/rick-owens

www.siteurl.com/video/rick-owens

然后将functions.php代码更改为

Then change the functions.php code to

$structure   = 'video/%rewrite%';

参见img以获取视频网址检查

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

If you have for city guide custom post type,

www.siteurl.com/city_guide/rick-owens

www.siteurl.com/city_guide/rick-owens

$structure1   = 'city_guide/%rewrite%';

查看img以获取城市指南网址检查

这是另一个选项,如果在我的项目中发生与您相似的任何冲突,我通常会这样做.如果没有,请在这篇文章中提及我.

This is the another option, which i usually do if any conflict occur as similar as yours in my project. If you didn't got it please mention me on this post.

希望这会起作用.

更新后的流程中的更新答案

Updated Answer as from your updated process

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

Please use this code for updating your post type to the Wordpress Core functionality

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' );

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

Hope this code will work for you and please remove the code as i mention earlier above and dont forget to update the permalink.

谢谢

注意:将代码放在其中后,不要忘记更新您的永久链接 functions.php

NOTE: Don't forget to update you permalink once you placed the code in functions.php

这篇关于带有相同条目的自定义帖子重定向到具有相同条目的错误帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆