• archive > single

    While on a single post, I build a list of other posts within same category. I display that list in a module on the single template. The current post that I’m looking at is displayed in that list as the first permalink. How do I remove the permalink from the list throughout my site?

    <ul>
                        <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                        <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4 ); ?>
                        <?php $queryObject = new WP_Query($query); ?>
                        <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                        <li><a href="<?php echo get_permalink(); ?>"><?php if (strlen($post->post_title) > 22) { echo substr(the_title($before = '', $after = '', FALSE), 0, 22) . '...'; } else { the_title();} ?>
                          </a></li>
                        <?php endwhile; else: ?>
                        <li>No related resources found.</li>
                        <?php endif; ?>
                        <?php wp_reset_query(); ?>
                      </ul>

    [No bumping. If it’s that urgent, consider hiring someone.]

Viewing 9 replies - 1 through 9 (of 9 total)
  • you get the current post Id, for instance with global $post; $postID = $post->ID; (you might not need the ‘global’ if you are in the single.php template);

    and exclude the post ID from the query with 'post__not_in' => array( $postID )

    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

    it might just work with:

    <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4, 'post__not_in' => array( $post->ID ) ); ?>
    Thread Starter danalydesign

    (@danalydesign)

    alchymyth, I appreciate your input but this does not seem to be working. When I’m viewing the single post, in my list module (which pulls the lates 4 post in the same category) this single post is still displaying as a link even though I’m viewing it. This is something that is happening on a global basis.

    Thread Starter danalydesign

    (@danalydesign)

    I found this back 3 years ago on here:

    <?php
    $yourcat = 373; // Stick the ID here and it'll be placed in the relevant places below.
    
    if( in_category( $yourcat ) ) :
    	$my_query = new WP_Query(array(
    		'showposts' => 3,
    		'orderby' => 'rand',
    		'cat' => $yourcat,
    		'post__not_in' => array($post->ID)
    	)); unset($yourcat);
    	while ($my_query->have_posts()) : $my_query->the_post();
    	?>
    		<a href="<?php the_permalink(); ?>"><img src="<?php echo p75GetThumbnail($post->ID, 100, 100); ?>"></a>
    	<?php
    	endwhile;
    endif;
    ?>

    how could i incorporate this into my code:

    <ul class="icons">
    					  <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                          <?php $query = array ( 'post_type' => 'articles', 'articletype' => 'news', 'category__in' => $post_categories, 'posts_per_page' => 4 ); ?>
                          <?php $queryObject = new WP_Query($query); ?>
                          <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                          <li class="icon-file"><a href="/articles/<?php echo basename(get_permalink()); ?>/"><?php if (strlen($post->post_title) > 33) { echo substr(the_title($before = '', $after = '', FALSE), 0, 33) . '...'; } else { the_title();} ?>
                            </a></li>
                          <?php endwhile; else: ?>
                          <li>No research articles found.</li>
                          <?php endif; ?>
                          <?php wp_reset_query(); ?>
                        </ul>

    When I’m viewing the single post, in my list module (which pulls the lates 4 post in the same category) this single post is still displaying as a link even though I’m viewing it

    what code exactly did you try?
    is that ‘list module’ directly coded into single.php or done with a function or widget?

    I found this back 3 years ago on here:

    because that code uses the same ‘post__not_in’ parameter, I would be surprised if that code would work where my suggestion does not work.

    Thread Starter danalydesign

    (@danalydesign)

    I used this
    <?php $query = array ( 'post_type' => 'affiliates', 'affiliatetype' => 'apps', 'category__in' => $post_categories, 'posts_per_page' => 4, 'post__not_in' => array( $post->ID ) ); ?>

    Thread Starter danalydesign

    (@danalydesign)

    is that ‘list module’ directly coded into single.php or done with a function or widget?

    This is a chunk of code in my custom taxonomy single page: single-taxonomy.php

    Thread Starter danalydesign

    (@danalydesign)

    a thought: would this have anything to do with the fact that I’m pulling from a sub category?

    possibly, the $post->ID is somehow changed before you call the code, and is not the one from the single post.

    try to check what it by printing it before the code;

    <?php echo $post->ID; ?>

    have you tried with <?php global $post; ?> ?

    Thread Starter danalydesign

    (@danalydesign)

    it’s returning the proper post id – I can’t get this thing to work at all.
    Display all posts related to category but do not include the currently displayed post in the list:

    <ul>
                        <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                        <?php $query = array ( 'post_type' => 'community', 'category__in' => $post_categories, 'posts_per_page' => 5 ); ?>
                        <?php $queryObject = new WP_Query($query); ?>
                        <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                        <li><a href="<?php echo get_permalink(); ?>"><?php if (strlen($post->post_title) > 22) { echo substr(the_title($before = '', $after = '', FALSE), 0, 22) . '...'; } else { the_title();} ?></a></li>
                        <?php endwhile; else: ?>
                        <li>No related resources found.</li>
                        <?php endif; ?>
                        <?php wp_reset_query(); ?>
                        <li class="last"><i class="icon-circle-arrow-right"></i> <a href="/community/category/sleep/">View All Resources</a></li>
                      </ul>
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Get posts from category – exclude current post’ is closed to new replies.