以下代码应为:如果当前自定义帖子类型(bbp\\U论坛)是正在显示的帖子类型,请分配class \'电流”至其各自<li>
标签但出于某种原因\'current\' (突出显示当前bbp_forum
链接)显示在所有<li>
标签:
<body <?php body_class(); ?>>
<div id="wrapper" class="hfeed">
<div id="header">
<div id="masthead">
<div id="branding" role="banner">
<h1><a href="<?php echo home_url( \'/\' ); ?>" title="<?php echo esc_attr( get_bloginfo( \'name\', \'display\' ) ); ?>" rel="home"><?php bloginfo( \'name\' ); ?></a></h1>
</div><!-- #branding -->
<div id="access" role="navigation">
<?php wp_nav_menu( array( \'container_class\' => \'menu-header\', \'theme_location\' => \'primary\' ) ); ?>
</div><!-- #access -->
</div><!-- #masthead -->
<ul id="forums">
<?php global $post; $cat_posts = get_posts(\'post_type=bbp_forum\');
foreach($cat_posts as $post) : ?>
<li <?php if($post->ID == get_the_ID()){ ?>class="current" <?php } ?>>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul><!-- #access -->
</div><!-- #header -->
<div id="main">
有什么建议吗?
最合适的回答,由SO网友:TheDeadMedic 整理而成
这句话永远是真的。看看get_the_ID()
;
function get_the_ID() {
global $post;
return $post->ID;
}
因此,您的代码有效地运行为;
if ( $post->ID == $post->ID ) // always true!
相反,将主帖子的ID缓存在变量中,然后进行比较。
<?php
global $post;
/**
* @var int Current post ID.
*/
$the_post_ID = $post->ID;
/**
* @var array All posts for bbp_forum.
*/
$cat_posts = get_posts(\'post_type=bbp_forum\');
?>
<?php foreach ( $cat_posts as $post ) : ?>
<li<?php if ( $post->ID == $the_post_ID ) echo \' class="current"\'; ?>>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>