2017-09-15 48 views
0

因此,這是我的代碼,它的工作是從郵政類型調用標題。因此,對於這個樣本的調用這類影片顯示郵政類型的所有標題,並在重定向時突出顯示當前頁面

一號
標題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(); ?> 
相關問題