Boomer Boomer - 2 months ago 5
PHP Question

Exclude current page from WP loop

I've built a navigation using simple HTML and Php. It's straightforward, but I'd like it to either:


  1. exclude the current post from the loop so it doesn't appear or

  2. have some sort of identifying style change to show it's the current page.



There's a couple of pages out there, but nothing I've tried is working out. Anybody have any ideas?

Code:



<div class="project-nav" style="display:none;">
<div class="nav-container">

<?php
$catPost = get_posts('cat=5&posts_per_page=-1000');
foreach ($catPost as $post) : setup_postdata($post); ?>

<a href="<?php the_permalink(); ?>">
<div class="nav-fixed">
<p><?php the_title(); ?></p>
</div>
</a>

<?php endforeach;?>
</div>
</div>




Answer

Try this:

<div class="project-nav" style="display:none;">
    <div class="nav-container"> 

    <?php
    $current_id =  get_the_ID();
    $catPost = get_posts('cat=5&posts_per_page=-1000');

    foreach ($catPost as $post) : setup_postdata($post);
        if ( get_the_ID() == $current_id ) {
            continue;
        }

    ?>
    <a href="<?php the_permalink(); ?>">
            <div class="nav-fixed">
                <p><?php the_title(); ?></p>

            </div>
    </a>

    <?php  endforeach;?>

</div>