問題描述

我有兩個作者頁面,一個顯示大約 5 個帖子。然後我試圖設定另一個頁面,將其全部的職位。我建立了一個名為 moreauthorposts.php 的模板,我試圖將作者變數傳遞給此頁面。問題是如果我透過 domain.com/more-author-posts?author=johndoe 它被剝離。如何檢索該值?這是甚麼可能在 wordpress?我知道 WP Rewrite 是我的 URL 結構,但我不知道是什麼。

我試過了:

get_query_var('author')

並嘗試閱讀,但沒有任何運氣:

http://codex.wordpress.org/Query_Overview

建議?

謝謝。

最佳解決方案

我幾乎肯定 author 是內建,所以使用像 author_more 這樣的東西。您將需要首先將該 var 新增到 query_vars 。例:

// add `author_more` to query vars
add_filter( 'init', 'add_author_more_query_var' );
function add_author_more_query_var()
{
    global $wp;
    $wp->add_query_var( 'author_more' );
}

然後在 more-author-posts.php 模板上呼叫它:

if ( get_query_var( 'author_more' ) )
{
    // do your stuff
}

Update

這在以下 URl 示例/用例中有效:

http://example.com/index.php?author_more=value

但是,如果要將其用作花哨的 URl,則需要新增重寫規則:

add_action('init','add_author_more_rewrite_rule');
function add_author_more_rewrite_rule()
{
    add_rewrite_rule(
        'more-author-posts/(d*)$',
        'index.php?author_more=$matches[1]',
        'top'
    );
}

現在你可以這樣使用

http://example.com/more-author-posts/value

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。