【问题标题】:Rewrite Wordpress url in CUSTOM POST TYPE在 CUSTOM POST TYPE 中重写 Wordpress url
【发布时间】:2015-09-30 07:37:04
【问题描述】:

我有星座帖子类型,我有 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 创建并且没有帖子
  2. 在 Monthly 和 Lovely 中,我需要在 url 显示: http://domain.com/horoscope/post-type/month-of-post-create/year-of-post-create/ 并且没有帖子
  3. 每周只:http://domain.com/horoscope/weekly/

【问题讨论】:

  • 您好,欢迎来到 stackoverflow,请查看 this,如果您需要帮助,请联系 tour,我们在这里为您提供帮助,而不仅仅是喂饱您..
  • 你能告诉我,我的帖子有什么问题吗?
  • 嗯,主要问题是你要代码,而你没有向我们展示任何自己尝试的努力..
  • 我想知道哪种方式编码更好。我很难过我认为更好的代码,但也许有人知道如何做得更好。并帮助我。
  • 所以首先我想咨询一下哪种结构更好!然后帮助编写代码。

标签: php wordpress url wordpress-theming


【解决方案1】:

现在我创建 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;

但是帖子类型 Monthly 和 Lovely 带有 %monthum% 的 url 被重定向到 404 页面并且不显示。

有什么更好的办法?

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2012-06-12
    • 2017-03-02
    • 2014-07-05
    • 2022-12-02
    • 2016-03-04
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多