簡體   English   中英

在自定義帖子類型中重寫 Wordpress url

[英]Rewrite Wordpress url in CUSTOM POST TYPE

我有星座帖子類型,我有 4 個類別(每年、每月、可愛和每周星座)每個帖子有 12 個編輯 - 12 個標志。 我需要那個 URL 結構:

  1. 在每年我需要這個網址: http : //domain.com/post-type/taxonomy/year
  2. 在每月我需要這個網址: http : //domain.com/post-type/taxonomy/month/year
  3. 在可愛我需要這個網址: http : //domain.com/post-type/taxonomy/month/year
  4. 在每周我需要這個網址: http : //domain.com/post-type/taxonomy

在每年我有很多年,在每月和可愛的我有很多年和很多月,在每周只在每周編輯的頁面上。

起初我認為“分類法” - 它將是分類法的父類別,“月” - 它將是分類法的子類別,而“年” - 它將是帖子。 但是當在分類法中創建相同的類別名稱時,他們的 slug 變成了“month-1,month-2”,當我在帖子類型中創建相同的帖子名稱時也是如此。 所以這個解決方案不起作用。

現在我想創建 4 個帖子類型,並在每個帖子類型中重寫 url,如下所示:

  1. 在每年我需要在 url 顯示中: http : //domain.com/horoscope/post-type/year-of-post create 並且沒有帖子
  2. 在 Monthly 和 Lovely 中,我需要在 url 中顯示: http : //domain.com/horoscope/post-type/month-of-post-create/year-of-post-create/並且沒有帖子
  3. 在每周只是: http : //domain.com/horoscope/weekly/

現在我創建 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;
}

對於年度帖子類型,我的網址中不需要 %monthum%,因此在該帖子中輸入代碼:

$kuukausihoroskooppi_structure = '/horoskoopit/kuukausihoroskooppi/%monthnum%/%year%/%kuukausihoroskooppi%';

我刪除了 %month%/ 並且這個帖子類型與 url http://domain.com/horoscope/post_type/year_of_post/post_slug配合使用很好,模板:single-post_type.php 但我不需要 post-slug,所以我刪除%kuukausihoroskooppi%,但是這個頁面顯示為archive.php;

但是帶有 %monthum% 的帖子類型 Monthly and Lovely url 被重定向到 404 頁面並且不顯示。

怎么辦比較好?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM