Declarația condițională if ($post->ID == get_the_ID) nu funcționează

14 mar. 2011, 21:21:23
Vizualizări: 16.6K
Voturi: 1

Următorul cod ar trebui să funcționeze astfel: Dacă tipul curent de postare personalizată (bbp_forum) este cel afișat, să atribuie clasa 'current' tag-ului <li> respectiv. Dar din anumite motive, clasa 'current' (pentru a evidenția link-ul curent bbp_forum) este afișată în toate tag-urile <li>:

captură de ecran cu problema

<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__( 'Legătură permanentă către %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
             </li>
          <?php endforeach; ?>
        </ul><!-- #access -->
    </div><!-- #header -->

    <div id="main">

Ce sugestii aveți?

0
Toate răspunsurile la întrebare 1
0

Expresia va fi întotdeauna adevărată. Uită-te la get_the_ID();

function get_the_ID() {
    global $post;
    return $post->ID;
}

Deci codul tău rulează efectiv ca:

if ( $post->ID == $post->ID ) // întotdeauna adevărat!

În schimb, salvează ID-ul postului principal într-o variabilă, apoi compară cu aceasta.

<?php

global $post;

/**
 * @var int ID-ul curent al postului.
 */
$the_post_ID = $post->ID;

/**
 * @var array Toate postările pentru 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__( 'Legătură permanentă către %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    </li>

<?php endforeach; ?>
14 mar. 2011 21:30:00