2017-09-15 9 views
0

これは自分のコードで、投稿タイプからタイトルを呼び出すための仕事です。だから、このサンプルのためにその本投稿タイプのタイトルをすべて表示し、リダイレクト時に現在のページをハイライト表示

タイトルI
タイトルII
のようなタイトル タイトルII

このリストを呼び出し、それが右の単一のページにリダイレクトし、私がクリックした場合、またリンクされている「タイトルII」が、私の問題は、現在の単一のページを強調表示していないということです。
タイトルI
タイトルII

タイトルI
タイトルII
タイトルII

が、私のコードの結果は次のようである:つまり、私はそれがこのように見えるようにしたいです タイトルII

以下は自分のページのコードですが、これも私のシングルページのコードです。

<?php $args = array( 
     'post_type' => 'services', 
     'posts_per_page' => -1 
    ); 
      $the_query = new WP_Query($args);?> 
      <?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 
       <li> 
        <a href="<?php the_permalink()?>"><?php echo the_title(); ?></a> 
       </li> 
      <?php endwhile?> 
      <?php endif; wp_reset_postdata();?> 

答えて

3

あなたはIDを使用して比較したいと思うかもしれません。ループ前に最新のIDを取得することを忘れないでください。

<?php 
// Remember to get ID before the loop to have current ID 
$current_post_ID = get_the_ID(); 
$args = array( 
    'post_type' => 'services', 
    'posts_per_page' => -1 
); 

$the_query = new WP_Query($args); 
?> 
<?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 
    <li> 
     <a <?php echo $current_post_ID === get_the_ID() ? 'class="active"' : '' ?> href="<?php the_permalink()?>"><?php echo the_title(); ?></a> 
    </li> 
<?php endwhile?> 
<?php endif; wp_reset_postdata();?> 
1

各投稿のリンクが現在のページのURLと一致するかどうかをテストする必要があります。

<?php 
global $wp; 
$current_url = home_url($wp->request) . '/'; 

$args = array(
    'post_type' => 'services', 
    'posts_per_page' => -1 
); 

$the_query = new WP_Query($args); ?> 

<?php if (have_posts()) : 
    while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <li <?php if ($current_url == get_the_permalink()) { echo 'class="active"'; } ?>> 
      <a href="<?php the_permalink()?>"><?php echo the_title(); ?></a> 
     </li> 
    <?php endwhile; ?> 
<?php endif; wp_reset_postdata(); ?> 
関連する問題