L'istruzione condizionale if ($post->ID == get_the_ID()) non funziona
14 mar 2011, 21:21:23
Visualizzazioni: 16.6K
Voti: 1
Il seguente codice dovrebbe verificare: Se il current custom post type (bbp_forum) è quello visualizzato, assegna la classe 'current' al rispettivo tag <li>
. Ma per qualche motivo la classe 'current' (per evidenziare il link del bbp_forum
corrente) viene visualizzata in tutti i tag <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 a %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul><!-- #access -->
</div><!-- #header -->
<div id="main">
Qualche suggerimento?

wyc
3.9K
Tutte le risposte alla domanda
1
0
L'espressione sarà sempre vera. Dai un'occhiata a get_the_ID()
:
function get_the_ID() {
global $post;
return $post->ID;
}
Quindi il tuo codice viene effettivamente eseguito come:
if ( $post->ID == $post->ID ) // sarà sempre vero!
Invece, memorizza l'ID del post principale in una variabile, poi confronta con quella.
<?php
global $post;
/**
* @var int ID del post corrente.
*/
$the_post_ID = $post->ID;
/**
* @var array Tutti i post per 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 a %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>

TheDeadMedic
36.6K
14 mar 2011 21:30:00
Domande correlate
1
risposte