web-dev-qa-db-ja.com

ページを使用せずにブログのカスタムサブパスを設定しますか?

警告:かなり前向きな要求です。

私は投稿(つまりブログ)と "howto"と呼ばれるカスタム投稿タイプを利用するデザインをしています。サイトのフロントページ( "/")には、最新のブログ投稿と最新のハウツーを表示するテンプレートがあります。ユーザーがブログセクション( "/ blog")に移動すると、howtoセクション( "/ howto")に移動するときとは異なるテンプレートが表示されます。

これを行う明白な方法は、2つのページ( "Blog"と "Home")を作成してから、[設定] - > [閲覧オプション]のドロップダウンを使用してそれらを明示的に設定することです。特定のページテンプレートをそれらの投稿に関連付けることができます。

しかし、それが本当に面倒ではないのであれば、気にする必要はありません。管理者側のページに実際のコンテンツが含まれているため、2つの役に立たない空のページが表示されます。これを不要にするためにテンプレートヒントを使用する方法があると思います。

  • / howto/ - > archive-howto.php
  • / blog/ - > archive.php

それで、本当に、私は質問が本当に以下の通りであると思います:

新しいページを作成せずに投稿のカスタムパスを設定する方法

5
aendrew

これについて詳しく説明する時間はありませんが(戻り次第)、その間はshould

Answer updated with explanation as promised.
WP Rewrite rules are like voodoo, I'm sure there's more than one way to go about this, but here's mine.

問題:

このスレッドにつまずくかもしれない他の人にあなたの質問を明確にするために、あなたがしたいことはページを作成します-なし物理的に空のプレースホルダーを作成する必要があります-pageの下にある管理ダッシュボード内:Page -> Add New

基本的に、Fake Pageを作成し、そのページで指定したテンプレートを使用するようにします。

溶液:

まず、書き換えルールを設定し、

add_action('init', 'fake_page_rewrite');

function fake_page_rewrite(){

    global $wp_rewrite;

    //set up our query variable %fake_page% which equates to index.php?fake_page= 
    add_rewrite_tag( '%fake_page%', '([^&]+)'); 

    //add rewrite rule that matches /blog/page/2, /blog/page/3, /blog/page/4, etc..
    add_rewrite_rule('^blog/page/?([0-9])?','index.php?fake_page=blog&paged=$matches[1]','top');  

    //add rewrite rule that matches /blog
    add_rewrite_rule('^blog/?','index.php?fake_page=blog','top');

    //add endpoint, in this case 'blog' to satisfy our rewrite rule /blog, /blog/page/ etc..
    add_rewrite_endpoint( 'blog', EP_PERMALINK | EP_PAGES );

    //flush rules to get this to work properly
    $wp_rewrite->flush_rules();

}

add_rewrite_tag内で、%fake_page%の形式でクエリ変数を指定します。これにより、必要に応じて、またはニーズに適したものを指定できます。私の例fake_pageは、この答えの仕組みを説明するための象徴的なものです。

このインスタンスでクエリ変数がどのように機能するかは、次のリクエストを照合することにより、

http://www.example.com/blog

...その後、内部的にマッピングされ、

http://www.example.com/index.php?fake_page=blog

後者は、デフォルトのパーマリンク構造を実行したときに表示されるものです。

同様の方法で、

http://www.example.com/blog/page/2
http://www.example.com/blog/page/3
http://www.example.com/blog/page/4
etc...

...各マップを同等のものにマッピングし、

http://www.example.com/index.php?fake_page=blog&paged=2
http://www.example.com/index.php?fake_page=blog&paged=3
http://www.example.com/index.php?fake_page=blog&paged=4
etc...

上記のスニペットの例では、最初にページング/blog/page/{page_number}に一致する書き換えルールがあり、2番目のルールに/blogの偽ページのベースに一致する書き換えルールがあることに気付くでしょう。

これは、ベースルールがblogとして定義されているエンドポイントの最初の出現で一致を返さないようにしてから、要求されたURLの残りを評価して、ユーザーが実際、ページングされた結果を要求しました。基本的に、これらのルールを逆にすると、機能しません。

前述のように、書き換えルールは私にとってはブードゥー教のようなものです。したがって、おそらく関数add_permastructの使用に関連するルールを指定する順序を変更する別の方法があります。誰かが代替手段を持っているなら、チャイムで!

template_redirectにフックする次の関数は、クエリ変数内にfake_pageが存在するかどうかを確認します。配列内に一致または存在する場合、目的のテンプレートファイルを含めて、データ。

add_action('template_redirect', 'fake_page_redirect');

    function fake_page_redirect(){

        global $wp;

        //retrieve the query vars and store as variable $template 
        $template = $wp->query_vars;

        //pass the $template variable into the conditional statement and
        //check if the key 'fake_page' is one of the query_vars held in the $template array
        //and that 'blog' is equal to the value of the key which is set
        if ( array_key_exists( 'fake_page', $template ) && 'blog' == $template['fake_page'] ) {

            //if the key 'fake_page' exists and 'blog' matches the value of that key
            //then return the template specified below to handle presentation
            include( get_template_directory().'/your-template-name-here.php' );

            exit;

        }
    }

PS. I have tested this and it works under my conditions, though I'm not sure if any other quirks may popup with rewrite rules and end point masks the deeper you go, therefore you should thoroughly test all pagination and permalinks and make sure that they resolve to their intended paths correctly . If not, we can address those issues as they arise.

9
Adam

2つのアーカイブページを作ります。

ブログの投稿にはarchive.php、ハウツーにはarchive-howto.phpを。

http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

偽のページを作成して書き換えを実行するのはやり過ぎで、やや危険です。

もちろん、私はその点を完全に見逃していない限り。

0