WordPress.org

Support

Support » How-To and Troubleshooting » query_posts – add class current Page – Child

query_posts – add class current Page – Child

  • I have a “navigation” with query_post and I want to add the class “current” when the child is the current page.

    query_posts('post_type=page&post_parent=37&order=asc'); while (have_posts()) : the_post (); ?>
    <li ADD CLASS HERE id="post-<?php the_ID(); ?>" > <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <img src="IMAGE HERE" alt="" />
    </li>
    <?php endwhile; wp_reset_query();

    I didn’t do it with register_nav_menus because I need to custom it a little with images.
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try this:

    $post_id = $post->ID;
    query_posts('post_type=page&post_parent=37&order=asc'); while (have_posts()) : the_post (); ?>
    <?php
    $class = '';
    if ($post_id == $post->ID){ $class = 'class="current" '; }
    ?>
    <li id="post-<?php the_ID(); ?>" <?php echo $class ?>> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <img src="IMAGE HERE" alt="" />
    </li>
    <?php endwhile; wp_reset_query();

    Excelent, that’s it ^^, thanks a lot n.n

    Thanks kees – been banging my head against the wall for a couple hours trying to find this solution.

    Where do you put this code, in the template file or functions.php?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘query_posts – add class current Page – Child’ is closed to new replies.
Skip to toolbar