2015-11-11 29 views
1

我正在一个WordPress网站上工作,我已经完成了很多,唯一我似乎无法弄清楚的是如何让WordPress知道我在某个帖子并设置了该链接的ID为“当前”。以下是我的代码,并且在'''标签中的if语句中有一些伪代码,用于我需要做的很多事情。任何帮助都会很棒。谢谢!使当前帖子链接的ID“当前”

<ul> 
    <?php query_posts(array('post_type'=>'project', 'posts_per_page' => 4));?> 
    <?php while (have_posts()) : the_post(); ?> 
    <li> <a href="<?php the_permalink(); ?>" <?php if(is_current_post()) { ?> id="current" <?php } ?>><?php the_title(); ?></a> 
    </li> 
    <?php endwhile; wp_reset_query(); ?> 
</ul> 

回答

2

试试这个代码。

<?php 
global $post; 
$currentPostId = $post->ID; 
?> 
<ul> 
    <?php query_posts(array('post_type'=>'project', 'posts_per_page'=>4)); ?> 
    <?php while (have_posts()) : the_post(); ?> 
    <?php $projectCurrentPostId = get_the_ID(); ?> 
    <li> 
     <a href="<?php the_permalink(); ?>" <?php if($currentPostId == $projectCurrentPostId) { ?> id="current" <?php } ?>> 
      <?php the_title(); ?> 
     </a> 
    </li> 
    <?php endwhile; wp_reset_query(); ?> 
</ul> 
+0

感谢很多人,你是A G – MrDevRI

+0

谢谢你的欣赏! –

1

试试这个:

$current_post_id = $post->ID; 

<ul> 
      <?php query_posts(array('post_type' => 'project', 'posts_per_page' => 4));?> 
      <?php while (have_posts()) : the_post(); ?> 
      <li><a href="<?php the_permalink(); ?>" <?php if($wp_query->current_post->ID == $current_post_id) { ?> id="current" <?php } ?>><?php the_title(); ?></a></li> 
      <?php endwhile; wp_reset_query(); ?> 
     </ul> 
+0

谢谢你这么多的投入,但它没有工作:( – MrDevRI