php - Rewrite Wordpress url in CUSTOM POST TYPE -
i have horoscope post type, have 4 categories (yearly, monthly, lovely , weekly horoscope) every post have 12 editors - 12 signs. need url structure:
- in yearly need url: http://domain.com/post-type/taxonomy/year
- in monthly need url: http://domain.com/post-type/taxonomy/month/year
- in lovely need url: http://domain.com/post-type/taxonomy/month/year
- in weekly need url: http://domain.com/post-type/taxonomy
in yearly have many years, in monthly , lovely have many years , many month, in weekly on page edit every week.
at first think "taxonomy" - parent category of taxonomy, "month" - child category of taxonomy , "year" - post. when create in on taxonomy same name of category slug became "month-1, month-2" , same when create same post name in post type. solution doesn't work.
now want create 4 post type , in each post type rewrite url, that:
- in yearly need in url show: http://domain.com/horoscope/post-type/year-of-post create , no slug of post
- in monthly , lovely need in url show: http://domain.com/horoscope/post-type/month-of-post-create/year-of-post-create/ , no slug of post
- in weekly just: http://domain.com/horoscope/weekly/
now create 4 post type , rewrite url code:
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 plugin init function add_filter('post_type_link', 'kuukausihoroskooppi_permalink', 10, 3); // adapted 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 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 assign 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 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 fancy permalink option } return $permalink; }
for yearly post type don`t need %monthum% in url in post type in code:
$kuukausihoroskooppi_structure = '/horoskoopit/kuukausihoroskooppi/%monthnum%/%year%/%kuukausihoroskooppi%';
i remove %month%/ , post type work url http://domain.com/horoscope/post_type/year_of_post/post_slug template: single-post_type.php don`t need post-slug, remove %kuukausihoroskooppi%, page show archive.php;
but post type monthly , lovely wich url %monthum% redirect 404 page , dont show.
what better do?
Comments
Post a Comment