如何生成自定义帖子类型年份存档重写规则? [英] How to generate custom post types year archive rewrite rules?

查看:108
本文介绍了如何生成自定义帖子类型年份存档重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在尝试像平常一样自己重写后(但这次是出于某种原因,没有运气),我找到了这个答案:

Ok, after trying to do it by myself as I usually do with rewrites (but this time for some reason, with no luck) I've found this answer:

自定义帖子类型的年度/每月存档

但仍然对我不起作用。
如果我记下规则,我会找到正确的规则,它会正确书写:

But still doesn't work for me. If I log the rules, I can find the right one, it gets correctly written:

[ it / press-release /([0-9] {4})/?$] =>
string(50) index.php?post_type = press_article& year = $ matches [1]

为了完整性,登录规则的第一行是(请看下面的最后一条,是正确的):

Just for completeness, the FIRST lines loggin the rules are (see the last one below, it's right):

array(309) {
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$"]=>
  string(87) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(104) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
  string(105) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/?$"]=>
  string(71) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$"]=>
  string(88) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]"
  ["it/press-release/([0-9]{4})/([0-9]{1,2})/page/([0-9]{1,})/?$"]=>
  string(89) "index.php?post_type=press_article&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]"
  ["it/press-release/([0-9]{4})/?$"]=>
  string(50) "index.php?post_type=press_article&year=$matches[1]"

但是仍然不起作用。如果我尝试导航到 it / press-release / 2016 (是的,有该日期的帖子),我将被重定向到首页。

But still it doesn't work. If I try to navigate to it/press-release/2016 (yes there are posts with that date) I'll get redirected to the front page.

我必须说几句话:


  • 我使用polylang

  • 我的自定义帖子类型是这样注册的:

  • I use polylang
  • My custom post type is registered like this:

$args = array(
   'public' => true,
   'supports' => array('title','editor','page-attributes'),
   'description' => 'Rassegna stampa',
   'labels' => $labels, // omitting here since not relevant
   'rewrite' => array('slug' => pll__('rassegna-stampa')),
   'has_archive' => true,
   'menu_icon' => 'dashicons-media-document'
);//end args

register_post_type('press_article',$args);


有人可以发现问题吗?

推荐答案

我在最近的WorPress项目中做了类似的事情,客户希望对新闻进行基于年份的导航(新闻类别中的帖子) 。我使用自定义重写规则来做到这一点。

I did something like that in my recent WorPress project where the client wanted to have year based navigation for news (posts in the category News). I used custom rewrite rules to do that.

注册自定义重写规则

function px_generate_rewrite_rules($wp_rewrite)
{
    $new_rules = array(
        '([^/]+)/archive/(\d+)/?$' => 'index.php?category_name=$matches[1]&px_archive_year=$matches[2]'
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'px_generate_rewrite_rules');

注册自定义查询变量

function px_query_vars($qvars)
{
    $qvars[] = 'px_archive_year';
    return $qvars;
}
add_filter('query_vars', 'px_query_vars');

添加自定义查询

function px_pre_posts($query) 
{

    if( ! is_admin() )
    {
        if( $query->is_main_query() && is_category() )
        {
            set_query_var('date_query', array(
                array(
                    array(
                        'year'  => get_query_var('px_archive_year', date('Y')),
                    )
                )
            ));
            return;
        }
    }
}
add_action('pre_get_posts', 'px_pre_posts');

请记住,如果您更改某些内容,则必须刷新重写规则。完成后,删除此代码。

Please remember that you have to flush rewrite rules if you change something. Remove this code when you're done.

add_action('init', function() {
    flush_rewrite_rules();
});

NB!请注意页面标题。每个页面都应该有一个唯一的标题。

NB! Please pay attention to the title of the page. Every page should have an unique title.

这篇关于如何生成自定义帖子类型年份存档重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆