2016-09-20 5 views
0

[OK]を、私が見つけた私は通常、(運ではなく、何らかの理由でこの時間)書き換えを行うよう自分でそれをやろうとした後、この答え:まだカスタムポストタイプ年アーカイブ書き換えルールを生成するには?

Custom post type yearly/ monthly archive

しかし、はしていません私のために働く。私はルールをログインした場合 することは、私は正しいものを見つけることができ、それが正しく書き込まれます:

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

ただ、完全を期すために、最初の行は、ルールが(それは正しいです、下の最後の1を参照)ですloggin:

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

誰かが問題を見つけることができますか?

答えて

0

私は最近のWorPressプロジェクトで、クライアントがニュース(カテゴリNewsの投稿)の年単位のナビゲーションをしたいと思っていました。私はこれを行うためにカスタム書き換えルールを使用しました。

登録カスタム書き換えルール

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

登録するカスタムクエリVAR(S)

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(); 
}); 

NB!ページのタイトルに注意してください。すべてのページには一意のタイトルが必要です。

+0

はい、私の主な問題は、ワードプレスが正しいテンプレートにリダイレクトしていないことです。 – Stratboy

+0

"template_include"フックを使用して、使用するテンプレートを決定することができます。 (https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include) – taunoha

+0

はい私はすでに試しましたが、不思議なことに、フックが実行される前にリダイレクトが行われています。言い換えれば、いくつかのリダイレクトが起こっています(> site_url> front_page with language)。もちろん、フックは最後のもの(home/lang、press_articleではなく)でのみ実行されます。したがって、間違ったurl/redirection、正しい振る舞いはテンプレートが決定されなければならない最後のリダイレクトにあるためです。だから私の本当の問題は間違ったリダイレクションで、テンプレートではなく、申し訳ありません。テンプレートの前に何か起こるので、間違ったテンプレートにもつながります。 – Stratboy