存档页面和单个帖子的自定义重写规则


8

我正在用几种自定义帖子类型构建一个Wordpress网站。每个职位类型都是不同的杂志。我也有杂志年份和期刊的自定义元字段。

我想拥有网址(使用archive.php):

http://my_site/magazine/name/
http://my_site/magazine_name/year/
http://my_site/magazine_name/year/issue/

我是通过代码实现的:

function magazine_rewrite_tag() {
    add_rewrite_tag('%issue_year%', '([0-9]{4})');
    add_rewrite_tag('%issue%', '([0-9])');
}
add_action('init', 'magazine_rewrite_tag', 10, 0);

和:

function magazine_rewrite_rules() {
    add_rewrite_rule('^([a-z]+)/([0-9]{4})/([0-9])?', 'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]', 'top');
    add_rewrite_rule('^([a-z]+)/([0-9]{4})?', 'index.php?post_type=$matches[1]&issue_year=$matches[2]', 'top');
}
add_action('init', 'magazine_rewrite_rules');

当然,我在archive.php中也有一个自定义查询。而且效果很好。

但是现在,我的单篇文章网址不起作用。默认情况下,它是:

http://my_site/magazine/post-title

但它显示了杂志中的所有文章,例如url(并且使用了archive.php模板):

http://my_site/magazine/

如何为单个自定义帖子添加正确的重写规则?我可能想以这种格式为单个帖子(仅针对选定的自定义帖子类型)提供网址:

http://my_site/magazine/year/issue/post-title

多点注释:(1)第四个参数是过滤器或操作采用的参数数量。并且10是默认优先级。在这种情况下,您可以将两者都保留下来…或尝试更大的数目。(2)您是否检查了生成的重写规则?还要确保在检查这些规则的顺序时检查它们的顺序。转储$GLOBALS['wp_rewrite']。(3)您是否补充query_vars 说可能会发生冲突?
kaiser

Answers:


4

我找到了解决方案!Monkeyman Rewrite Analyzur插件非常有用:https://wordpress.org/plugins/monkeyman-rewrite-analyzer/

所以现在,我有这样的工作网址...

single.php:

/magazine-name/issue-year/issue/article-name

archive.php:

/magazine-name/issue-year/issue
/magazine-name/issue-year
/magazine-name

在wp-admin中的自定义帖子编辑器页面上,我将“高级自定义字段”插件用于发行年份和发行日期。您也可以自己定义元字段。

接下来,我添加了发行年份和发行日期的重写标签:

function custom_rewrite_tag() {
    add_rewrite_tag('%issue_year%', '([0-9]{4})' );
    add_rewrite_tag('%issue%', '([0-9]+)' );
}

add_action('init', 'custom_rewrite_tag');

接下来,我为URL的任何组合添加了重写重写规则:

function custom_rewrite_rule() {
    // URL: /magazine/year/issue/title
    add_rewrite_rule('^([^/]+)/([0-9]{4})/([0-9])/(.?.+?)?(:/([0-9]+))?/?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]&name=$matches[4]', 'top');

    // URL: /magazine/year/issue
    add_rewrite_rule('^([^/]+)/([0-9]{4})/([0-9])?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]&issue=$matches[3]', 'top');

    // URL: /magazine/year
    add_rewrite_rule('^([^/]+)/([0-9]{4})?$', 'index.php?post_type=$matches[1]&issue_year=$matches[2]', 'top');
}

add_action('init', 'custom_rewrite_rule');

最后,我将自己的标准网址替换为发行年份和发行日期

function custom_permalink($url, $post) {
    if ($post->post_type == 'magazine-name-1' || $post->post_type == 'magazine-name-2' || $post->post_type == 'magazine-name-2' ) {
        global $post;

        $post_type = $post->post_type;
        $issue_year = get_field('issue_year', $post->ID); // ACF; for meta: get_post_meta($post->ID, '$issue', true);
        $issue = get_field('issue', $post->ID); // ACF

        $url = str_replace( $post_type . '/', $post_type . '/' . $issue_year . '/' . $issue . '/' , $url);
    }
    return $url;
}
add_filter('post_type_link', 'custom_permalink', 10, 2);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.