2016-03-16 7 views
9

これは私の設定です。Wordpress next_post_link/previous_post_linkは同じカテゴリに属していません

single.php

<?php 

    if (in_category('my-category')) { 
     include(TEMPLATEPATH.'/single-my-category.php'); 
    } 
    else { 
     include(TEMPLATEPATH.'/single-generic.php'); 
    } 
?> 

シングル私の-category.php

<?php 

if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php echo the_title(); ?> 

<div class="pagination"> 
    <div class="container"> 
     <div class="row"> 
      <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php next_post_link('%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true); ?> 
      </div> 

      <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php previous_post_link('%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true); ?> 
      </div> 
     </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 

これは私が続いてきたものである - http://codex.wordpress.org/Function_Reference/next_post_link

私は私は何をかなりわからないんだけど関数のin_same_termパラメータがtrueに設定されていても、何らかの理由でここで間違っていると、previous_post_linkは別のカテゴリの投稿に私を連れて行っています。

アイデア?

ありがとうございました。

+0

-

はそれを試してみます。 [このリンクはこちら](http://wordpress.mcdspot.com/2010/06/02/stay-in-category/)をチェックしてください。また、[この回答](http://stackoverflow.com/a/3005646/629127)役立つ可能性があります。 –

+0

投稿は1カテゴリにのみ割り当てられます –

+0

ループ – StreetCoder

答えて

-1

このコードをテンプレートファイルに追加できますか?あなたは以下の通り次/前のリンクチェックthis link

+1

これらはCustomizr固有のフックです。 wordpressではなく、これは動作しません。 –

+0

Stackoverflows期限切れ賞金の選択プロセスは、この回答に賞金を与えました。この「解決策」はうまくいかない。ありがとう。 –

2

編集の詳細オプションのコード・ブロックが必要な場合

<ul class="pager"> 
    <?php if (get_previous_post() != null) : ?> 
    <li class="previous"> 
     <span class="nav-previous"> 
     <?php 
      $singular_nav_previous_text = apply_filters('tc_singular_nav_previous_text', _x('&larr;' , 'Previous post link' , 'customizr')); 
      previous_post_link('%link' , '<span class="meta-nav">' . $singular_nav_previous_text . '</span> %title'); 
     ?> 
     </span> 
    </li> 
    <?php endif; ?> 
    <?php if (get_next_post() != null) : ?> 
    <li class="next"> 
     <span class="nav-next"> 
     <?php 
      $singular_nav_next_text = apply_filters('tc_singular_nav_next_text', _x('&rarr;' , 'Next post link' , 'customizr')); 
      next_post_link('%link' , '%title <span class="meta-nav">' . $singular_nav_next_text . '</span>'); 
      ?> 
     </span> 
    </li> 
    <?php endif; ?> 
</ul> 

は今function.php

add_filter('tc_previous_single_post_link_args', 'navigate_in_same_taxonomy'); 
add_filter('tc_next_single_post_link_args', 'navigate_in_same_taxonomy'); 
function navigate_in_same_taxonomy($args){ 
    $args['in_same_term'] = true; 
    return $args; 
} 

にコードの下に追加します。 single.phpファイルの5行目に '.php'を入れていません。前/次の投稿はif single.phpの文(ここではカテゴリ 'php')内で指定したカテゴリの投稿のみに表示されます。次の例では、 'template-parts'ディレクトリを作成し、そのディレクトリ内に2つのphpファイル( "single-my-category.php"と "single-generic.php")を作成しました。

シングル私の-category.php

<?php 
    $the_query = new WP_Query($post); 

    if (in_category('php')) { 
     include('template-parts/single-my-category.php'); 
    } else { 
     include('template-parts/single-generic.php'); 
    } 
?> 

single.php

if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php the_title(); ?> 

<div class="pagination"> 
    <div class="container"> 
     <div class="row"> 
      <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php next_post_link('%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true); ?> 
      </div> 

      <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php previous_post_link('%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true); ?> 
      </div> 
     </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 
+0

こんにちは、答えてくれてありがとうございますが、これは実際に私のところではエラーです。私は誰もが私がやったことを見て、私は間違って最後に '.php'を外したことが容易になるように質問を整理しました。コードでは、実際にはテンプレート名の最後に '.php'があります。今私の質問でこれを編集します。 –

+1

@AndrewMatthew私はあなたがここで非常に重要な情報を残していると思います。あなたのコードは正常に動作するので、ポストされていないものはあなたのコードを破ることです。 –

+0

'next_post_link()'と 'previous_post_link()'は5つのパラメータをとります。 4番目は$ excluded_terms、5番目は$ taxonomyです(デフォルト値は 'category')。両方の機能にさらに2つのパラメータを追加してみることができますか? 4位は ''、5位は 'カテゴリー'となります。 [参考](https://developer.wordpress.org/reference/functions/next_post_link/) –

1

また、シングルcategoryname.phpが修正されていないので、あなたは試してみてください。..単一のページでカテゴリ名を指定します分類-taxonomy_name.php OR

<?php query_posts('category_name=CATEGORYNAME&showposts=5'); 
while (have_posts()) : the_post(); 
    // do whatever you want 
?> 
<?php endwhile;?> 
<div class="pagination"> 
<div class="container"> 
    <div class="row"> 
     <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
      <?php next_post_link('%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true); ?> 
     </div> 

     <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
      <?php previous_post_link('%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true); ?> 
     </div> 
    </div> 
</div> 

を使用して
-1

in_same_termが機能しない場合は、クエリオブジェクトが投稿データを保持していない可能性があります。あなたのポストの上に複数のカテゴリを持っているだけで、これまでポスト(推測)の最初のあるものになりWPので、これが起こる

global $the_query; 
$the_query = new WP_Query($post); 

global $the_query; 
if ($the_query->have_posts()) : while ($the_query->have_posts()) : the_post(); ?> 

<?php the_title(); ?> 

<div class="pagination"> 
    <div class="container"> 
     <div class="row"> 
      <div class="next col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php next_post_link('%link', '<img src="' . get_template_directory_uri() . '/img/cs-left.png" /> PREVIOUS', true); ?> 
      </div> 

      <div class="previous col-xs-6 col-sm-6 col-md-6 col-lg-6"> 
       <?php previous_post_link('%link', 'NEXT <img src="' . get_template_directory_uri() . '/img/cs-right.png" />', true); ?> 
      </div> 
     </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 
関連する問題