[英]Rewrite Wordpress url in CUSTOM POST TYPE
I have horoscope post type, where I have 4 categories (yearly, monthly, lovely and weekly horoscope) every post have 12 editors - 12 signs.我有星座帖子类型,我有 4 个类别(每年、每月、可爱和每周星座)每个帖子有 12 个编辑 - 12 个标志。 I need that URL structure:
我需要那个 URL 结构:
In Yearly I have many years, in monthly and lovely I have many years and many month, in weekly just on page that edit every week.在每年我有很多年,在每月和可爱的我有很多年和很多月,在每周只在每周编辑的页面上。
At first I think that "taxonomy" - it will be parent category of taxonomy, "month" - it will be child category of taxonomy and "year" - it will be post.起初我认为“分类法” - 它将是分类法的父类别,“月” - 它将是分类法的子类别,而“年” - 它将是帖子。 But when create in on taxonomy the same name of category their slug became "month-1, month-2" and the same when I create the same post name in post type.
但是当在分类法中创建相同的类别名称时,他们的 slug 变成了“month-1,month-2”,当我在帖子类型中创建相同的帖子名称时也是如此。 So this solution doesn't work.
所以这个解决方案不起作用。
Now I want to create 4 post type and in each post type rewrite url, like that:现在我想创建 4 个帖子类型,并在每个帖子类型中重写 url,如下所示:
Now I create 4 post type and rewrite url with this code:现在我创建 4 个帖子类型并使用以下代码重写 url:
global $wp_rewrite;
$kuukausihoroskooppi_structure = '/horoskoopit/kuukausihoroskooppi/%monthnum%/%year%/%kuukausihoroskooppi%';
$wp_rewrite->add_rewrite_tag("%kuukausihoroskooppi%", '([^/]+)', "kuukausihoroskooppi=");
$wp_rewrite->add_permastruct('kuukausihoroskooppi', $kuukausihoroskooppi_structure, false);
// Add filter to plugin init function
add_filter('post_type_link', 'kuukausihoroskooppi_permalink', 10, 3);
// Adapted from get_permalink function in wp-includes/link-template.php
function kuukausihoroskooppi_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
$leavename? '' : '%postname%',
'%post_id%',
'%category%',
'%author%',
$leavename? '' : '%pagename%',
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$unixtime = strtotime($post->post_date);
$category = '';
if ( strpos($permalink, '%category%') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, '_usort_terms_by_ID'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, '/', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos($permalink, '%author%') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date_i18n('Y F d H i s', $unixtime));
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
} else { // if they're not using the fancy permalink option
}
return $permalink;
}
For Yearly post type I don`t need %monthum% in my url so in that post type in code:对于年度帖子类型,我的网址中不需要 %monthum%,因此在该帖子中输入代码:
$kuukausihoroskooppi_structure = '/horoskoopit/kuukausihoroskooppi/%monthnum%/%year%/%kuukausihoroskooppi%';
I remove %month%/ and this post type work good with url http://domain.com/horoscope/post_type/year_of_post/post_slug with template: single-post_type.php But I don`t need post-slug, so I remove %kuukausihoroskooppi%, but then this page show like archive.php;我删除了 %month%/ 并且这个帖子类型与 url http://domain.com/horoscope/post_type/year_of_post/post_slug配合使用很好,模板:single-post_type.php 但我不需要 post-slug,所以我删除%kuukausihoroskooppi%,但是这个页面显示为archive.php;
But post type Monthly and Lovely wich url with %monthum% is redirect to 404 page and dont show.但是带有 %monthum% 的帖子类型 Monthly and Lovely url 被重定向到 404 页面并且不显示。
What better to do?怎么办比较好?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.