WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Strange request! I need to NOT display multiple posts of the same name (7 posts)

  1. semidivine
    Member
    Posted 4 years ago #

    In other words, my client has multiple posts of the same name and he wants to output all the post titles from a category except if it's a repeat.

    Here's an example:

    Category X has these posts:
    foo
    bar
    bar
    word
    press
    press
    press

    and the client wants this to display:
    foo
    bar
    word
    press

    How do I do this?? I am flummoxed!

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    more details?

    where do you need the output?
    in a category list?
    on a category archive?
    is the output done by a loop?
    or in a widget?

    if in a loop, the task is easy:
    you could collect the post titles displayed so far in an array, and then exclude those that are already stored in this array from being output again:

    if (have_posts()) :
    $all_titles = array(); // initiallize array
    while (have_posts()) : the_post(); 
    
    if(!in_array(get_the_title(),$all_titles)) { the_title(); } // only output title if not done before
    $all_titles[] = get_the_title(); // update array
    
    endwhile;
    endif;
  3. semidivine
    Member
    Posted 4 years ago #

    You are a genius! What you gave me was clear enough that I could adapt it to my script, which has a lot more going on. Basically it's outputting a nav of posts.

    There is one more thing maybe you can help me with... I have it so that the posts display in columns of seven, which worked great except now it's counting the duplicate posts that aren't showing up and so in one example the new column starts only after 4 displaying posts:

    http://dev.identifidesign.com/print/

    My code is this:

    <div class="col">
    <ul>
    <?
    	$postCounter=0;
    	global $initialPostID;
    	$postid = $post->ID;
    
    	query_posts('category_name='.$category);
    	if (have_posts()) :
    		$all_titles = array(); // initiallize array
    		while (have_posts()) : the_post();
    			if(!in_array(get_the_title(),$all_titles)) :
    				$postidCat = $post->ID;
    				if (!$initialPostID) :
    ?>
    <li<? if ($postid==$post->ID) : echo " class=\"current_page_item\""; $postid = ""; endif; ?>><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?
    				else:
    ?>
    <li<? if ($postidCat == $initialPostID ) : echo " class=\"current_page_item\""; $postid = ""; endif; ?>><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?
    				endif;
    			endif;
    			$all_titles[] = get_the_title(); // update array
    			$postCounter=$postCounter + 1;
    			if ($postCounter == 7) :
    ?>
    </ul>
    </div>
    <div class="col">
    <ul>
    <?
    			endif;
    		endwhile;
    	else:
    	endif;
    	//Reset Query
    	wp_reset_query();
    ?>
    </ul>
    </div>

    How do I get it so that 7 visible posts show up before starting a new column? Thanks!

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    i think you could move the $postCounter increment to a different position, so it only increments if you output a title -

    from this:

    endif;
    			endif;
    			$all_titles[] = get_the_title(); // update array
    			$postCounter=$postCounter + 1;
    			if ($postCounter == 7) :

    to this:

    endif;
    			$postCounter=$postCounter + 1;
    endif;
    			$all_titles[] = get_the_title(); // update array
    						if ($postCounter == 7) :

    hope you can figure out what i mean, good luck ;-)

    (always make a backup copy before editing theme files)

  5. semidivine
    Member
    Posted 4 years ago #

    I did figure it out, you are very clear, thanks. But as soon as I moved it (I tried your suggested place plus a couple other placements in the code) the columns of 7 stopped working. Not quite sure what to do... my crazy brain is going to calculating how many posts are hidden by your script and doing some math to add that number dynamically to:
    if ($postCounter == 7) :
    so it would be:
    if ($postCounter == (7 + $x)) :
    but I'm not sure how to calculate $x.

    Do you think that might work?

  6. alchymyth
    Forum Moderator
    Posted 4 years ago #

    i think, i had the postCounter incremant at the right spot; however, further down, the code was missing a reset of the postCounter.

    this bit seems to do it now (im my local test setup):

    <?php
    				endif;
    				$postCounter=$postCounter + 1;
    			endif;
    			$all_titles[] = get_the_title(); // update array
    
    			if ($postCounter == 7) :
    ?>
    </ul>
    </div>
    <div class="col">
    <ul>
    <?php $postCounter = 0; //reset counter
    			endif;
    		endwhile;
    	else:
    	endif;
    	//Reset Query
    	wp_reset_query();
    ?>

    good luck ;-)

  7. semidivine
    Member
    Posted 4 years ago #

    Yay!! That works!! Thank you so much!!

    Phew!

    :)

Topic Closed

This topic has been closed to new replies.

About this Topic