按自定义字段' meta_key',' meta_value'进行过滤存档

时间:2014-12-03 04:47:12

标签: wordpress custom-fields

我使用自定义字段'meta_key' => 'author_name'作为访客帖子(没有用户个人资料),我手动输入每个帖子的作者姓名值。

是否可以过滤'meta_key''meta_value'作为链接:

example.com/author_name/guest001/ 要么 example.com/?author_name=guest001

并显示帖子,以便与类别,标签或真实作者非常相似?

1 个答案:

答案 0 :(得分:1)

首先你需要像这样添加重写标签:

function createRewriteRules() {
    global $wp_rewrite;

    // add rewrite tokens
    $authorname = '%authorname%';
    $wp_rewrite->add_rewrite_tag($authorname, '(.+?)', 'author-name=');

    $keywords_structure = $wp_rewrite->root . "author-name/$authorname/";
    $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);

    $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
    return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'createRewriteRules');

然后钩子生成规则并添加重写规则:

function add_rewrite_rules( $wp_rewrite ) 
{
        add_rewrite_rule('^author-name/([^/]*)/?','index.php?page_id=12&author-name=$matches[1]','top');
        //page_id is added hardcoded but you can get the page id using your own code/function
}
add_action('init', 'add_rewrite_rules');

将变量添加到查询变量:

function query_vars($public_query_vars) {
    $public_query_vars[] = "author-name";
    return $public_query_vars;
add_filter('query_vars', 'query_vars');

它应该可以使用,请使用Developer插件检查您的重写规则。