繁体   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