Wordpress Discussion

Out of context: Reply #3

  • Started
  • Last post
  • 164 Responses
  • fadein110

    In fact I will kick it off - I need a little advice on a loop:

    I have everything working in this loop except excluding the post that you are viewing the loop on - so this is kind of a related posts loop from a custom post type. Got it all working apart from excluding the current post - random works fine etc.

    Any ideas what I have missed?

    query_posts($the_query);

    // Reset and setup variables
    $output = '';
    $temp_title = '';
    $temp_link = '';
    $temp_url = '';

    // the loop

    $exclude = $GLOBALS['current_id'];

    $args = array(

    'post__not_in' => array($exclude), 'post_type'=>'our-work', 'orderby'=>'rand', 'posts_per_page'=>'3'

    );
    query_posts($args);
    if ( have_posts() ) : while ( have_posts() ) : the_post();

    $temp_title = get_the_title($post->ID);
    $temp_link = get_permalink($post->ID);
    $img_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

    // output all findings - CUSTOMIZE TO YOUR LIKING
    $output .= "<li class='workitem workitem2' style='background:url($img_url); background-repeat: no-repeat!important;
    background-size: cover!important;
    background-position: center center!important;'><a href='$temp_link'><span>$temp_title</span></a></li>

    • what are you trying to do?pockets
    • All I got is how to exclude the current post from the loop?pockets
    • its a related posts feed on a post page - but basically I want to pull in 3 random posts excluding the 1 you are currently viewing.fadein11
    • is it working but displaying current post on related posts loop?pockets
    • there's one way you can do it off the top of my head, but the post limit would vary, you just need to call the function that can pull the current id of...pockets
    • ..of the random related posts and exclude the current post from it. I'll write you an example, and you replace the missing variablespockets
    • thanks mate!fadein11

View thread