【问题标题】:How to generate custom post types year archive rewrite rules?如何生成自定义帖子类型年份存档重写规则?
【发布时间】:2017-01-28 02:51:03
【问题描述】:

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

Custom post type yearly/ monthly archive

但仍然不适合我。 如果我记录规则,我可以找到正确的,它会被正确写入:

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

为了完整起见,登录规则的第一行是(见下面最后一行,没错):

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(是的,有那个日期的帖子)我会被重定向到首页。

我必须说几件事:

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

    $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);
    

有人能发现问题吗?

【问题讨论】:

    标签: php wordpress url-rewriting custom-post-type


    【解决方案1】:

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

    注册自定义重写规则

    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');
    

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

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

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

    【讨论】:

    • 是的,但我的主要问题是 wordpress 没有重定向到正确的模板。
    • 您可以使用“template_include”挂钩来确定要使用的模板。 (codex.wordpress.org/Plugin_API/Filter_Reference/…)
    • 是的,我已经尝试过了,但奇怪的是,重定向是在钩子执行之前完成的。或者,换句话说,发生了一些重定向(> site_url > front_page with language),当然钩子只在最后一个(home/lang,而不是 press_article)上执行。因此,错误的 url/重定向,正确的行为,因为它是在必须确定模板的最后一次重定向上。所以我真正的问题是错误的重定向,而不是模板,对不起。在模板之前发生的事情,因此也会驱动到错误的模板。
    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2019-12-09
    • 2018-06-20
    相关资源
    最近更新 更多