I am running a custom query for my recent posts, code can bee seen at:
http://pastebin.com/CFZdP3c9

This code is just for my recent posts, not the actual page content.
My problem is that it shows my current page post item and I cannot figure out how to pass over it.

originally I tried

global $post;
$currentPost = $post->ID;
 if( $post->ID != $currentPost ) ?>
    (the loop)
endif;

but could not seem to get it to work correctly, it just didn't output anything.

Any insights are greatly appreciated!

Recommended Answers

All 8 Replies

Your question isn't amazingly clear; but if I'm understanding it correctly you'd like your recent posts?

<?php wp_get_recent_posts( $args, $output ) ?>

Example below will show 10 recent posts:

<h2>Recent Posts</h2>
<ul>
<?php
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $recent ){
        echo '
                <li>
                    <a href="'.get_permalink($recent["ID"]).'" title="Look .esc_attr($recent["post_title"]).'">' .   
                        $recent["post_title"].'
                    </a> 
                </li> 
            ';
    }
?>
</ul>

See ref

I can manage to get the recent post no problem, my problem is that when in single.php I have my post, lets say it has id=1

Now in my footer I have the code that appears in pastebin.
This code brings back my posts that I require with my specifc settings.

The problem I have is that I do NOT want the the post that id=1 to appear, other wise I have a link to page I am alrady on.

Doeas that make more sense?

Hello, thanks all, I re-wrote and looks similar to above, so I now have:

<h2>More News</h2>
            <?php
                global $post;
                $currentPost = $post->ID;
                 // store category in loc var via slug name
                $category = get_category_by_slug('uncategorized');
                // get category id from slug
                $catID = $category->term_id;

                $args = array( 'posts_per_page' => 3, 'exclude' => $currentPost, 'category' => $catID, );
                $lastposts = get_posts( $args );
                foreach($lastposts as $post) : setup_postdata($post); ?>
                    <div <?php post_class(); ?>>
                        <h3><?php the_title();?></h3>
                            <div>
                                <?php // Post thumbnail conditional display.
                                if ( bootstrapwp_autoset_featured_img() !== false ) : ?>
                                <div>
                                    <a href="<?php the_permalink(); ?>" title="<?php  the_title_attribute( 'echo=0' ); ?>">
                                        <?php echo bootstrapwp_autoset_featured_img(); ?>
                                    </a>
                                </div><!-- /inner plain div -->

                                <?php endif; ?>
                                <?php the_excerpt(); ?>
                                <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">
                                    Read full story
                                </a>
                                <?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) { ?><div class="clear"><hr /></div><?php } ?>
                            </div><!-- /plain div -->
                    </div><!-- /.post_class -->
                <?php endforeach; wp_reset_query();?>

Only now this line doesn't work:

<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) { ?><div class="clear"><hr /></div><?php } ?>

So this basically adds a hr if there is another post to follow. except it is adding it even on the last post. Once that is corrected i'm done ;-)

Change

($wp_query->current_post + 1) < ($wp_query->post_count)

to

($wp_query->current_post) < ($wp_query->post_count)

see if that works.

Member Avatar for diafol

I'm not into this WP stuff, but if you have a list of records in an array and you have the key which you don't want displayed - that's easy in normal php:

$dont_show = 7;

foreach($array as $key=>$items)
{
    if($key != $dont_show) echo $items['name'];
}

You're spot in diafol but WP has all of these functions built in and it's better to use them as then your WP site will work with any plugin or theme. When you start going beskope you get grey hairs :)

Member Avatar for diafol

Yes, so modify the built-in functions for this. Like I said, I'm not too familiar with WP, but you can surely extend a class or add a method to an existing class.

//on second reading

It seems that this method has the functionality I was expecting, without using the foreach loop, as we have this:

'exclude' => $currentPost

parameter.

I don't see why the last entry is based on current_post, but there again...

$cnt=0

before the foreach, then in the loop:

<?php if( $cnt < $wp_query->post_count) { ?><div class="clear"><hr /></div><?php } ?>
</div><!-- /plain div -->
</div><!-- /.post_class -->
<?php 
$cnt++;
endforeach; wp_reset_query();?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.