My carousel loop is getting the featured image attached to four posts with the category of “frontpage.”
The Bootstrap carousel needs the first div to have the class of “active,” so I am looping through one post to get that; then I’m using “do not duplicate” to skip the first post when I loop through the others.
I have this code:
<div class="container col-xs-6 col-sm-4">
<div class="carousel slide" id="featured">
<ol class="carousel-indicators" style="bottom:-50px;">
<li style="background-color:#ccc;" data-target="#featured" data-slide-to="0" class="active"></li>
<li style="background-color:#ccc;" data-target="#featured" data-slide-to="1"></li>
<li style="background-color:#ccc;" data-target="#featured" data-slide-to="2"></li>
<li style="background-color:#ccc;" data-target="#featured" data-slide-to="3"></li>
</ol>
<div class="carousel-inner">
<?php $args = array( 'category_name' => 'frontpage', 'posts_per_page' => 1 ); ?>
<?php $my_query = new WP_Query( $args );
while ( $my_query->have_posts() ) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="item active">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<?php endwhile; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
if ( $post->ID == $do_not_duplicate ) continue; ?>
<div class="item">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<?php endwhile; endif; ?>
</div><!-- carousel-inner -->
<a class="left carousel-control" href="#featured" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"> </span>
</a>
<a class="right carousel-control" href="#featured" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"> </span>
</a>
</div><!-- carousel -->
</div><!-- container -->
The problem is the second loop is picking up posts from all categories, not just those with the frontpage category.
If I change the first line of the second loop to:
<?php if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post();
if ( $post->ID == $do_not_duplicate ) continue; ?>
… I break it. I don’t get the last three images at all.
If this is the wrong approach, I’m happy to try something else. Thank you.
]]>I have this:
<?php
// The Query
query_posts( 'post_type=fellow' );
// The Loop
while ( have_posts() ) : the_post();
?>
<li><a href="#"><?php
$fellowship = get_post_meta( get_the_ID(), 'fellowship', true );
// check if the custom field has a value
if( ! empty( $fellowship ) ) {
echo $fellowship;
}
?></a></li>
<?php
endwhile;
?>
I’d like to set this up so that a fellowship echos only once, no matter how many times it might show up in the query.
I did this same thing a year ago using some different code, but I don’t think this was the best solution, it would be nice to be able to just use the query that’s already there and to do something like:
if the $fellowship has already appeared, skip it.
Here’s the code I used a year ago, in a totally different site:
$args = array(
'posts_per_page' => -1,
'category' => get_query_var( 'cat' ),
'order' => 'ASC',
'orderby' => 'title'
);
$brand_list = array();
$posts_array = get_posts($args);
foreach( $posts_array as $post ) : setup_postdata($post);
$brand = get_post_meta( $post->ID, 'brand', true);
if(!in_array($brand,$brand_list)){
array_push($brand_list,$brand);
$key_1_value = get_post_meta( get_the_ID(), 'brand', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
?>
<li class="menu-item menu-item-type-taxonomy menu-item-object-category"><a href="#" title="<? the_title() ?>" class="<?php
$brandnav = get_post_meta(get_the_ID(), 'brand', true);
$brandnav_array = explode(' ', $brandnav);
$first_word = $brandnav_array[0];
echo $first_word;
?>
">
<?php
echo $key_1_value;
?>
</a></li>
<?php
}
}
endforeach;
?>
Any ideas? Thank you.
https://192.185.31.23/~frontpor/wordpress/?page_id=15#
William in Roanoke, Va
]]>Any help much appreciated!
<?php $do_not_duplicate = array(); ?>
<!-- Featured Posts -->
<?php
$my_query = new WP_Query('tag=sticky&posts_per_page=3');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<-- Outputs three posts here-->
<?php endwhile; ?>
<!-- Normal Loop -->
<?php if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<-- Outputs the rest of the posts here-->
<?php endwhile; endif; ?>
<!-- But there are still duplicates? -->
]]><?php
/*
Template Name: nosidebar3
*/
?>
<?php // Do not delete these lines ?>
<?php /* get all options: */
# error_reporting(-1);
include (TEMPLATEPATH . '/functions/bfa_get_options.php');
global $bfa_ata;
$left_col2 = "off";
$left_col ="off";
$right_col2 = "off";
$right_col = "off";
get_header(); ?>
<div style="margin:0 auto;">
<table style="text-align:left; width:100%; margin-left:0px auto;
margin-right:0px auto;" border="1"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width:33%;">
<?php $accordion1 = new
WP_Query('category_name=AccordionPg1&showposts=3');
while($accordion1->have_posts()) : $accordion1->the_post();
$do_not_duplicate[] =$post->ID ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</td>
<td style="width:33%;" align="undefined"
valign="undefined">
<?php endwhile; ?>
</td></tr>
<tr><td style="width:33%;" align="undefined"
valign="undefined">
<?php rewind_posts(); ?>
<?php $accordion2 = new
WP_Query('category_name=AccordionPg1&showposts=3');
while($accordion2->have_posts()) : $accordion2->the_post();
if (in_Array($post->ID, $do_not_duplicate)) continue;
update_post_caches
($posts); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</td>
<td style="width:33%;" align="undefined"
valign="undefined">
<?php endwhile; ?>
</td>
<td style="width:33%;" align="undefined"
valign="undefined">
</tbody>
</table>
</div>
<?php get_footer(); ?>
]]>I first want to show the 4 latest posts. Then I want to show the latest posts from each category. I’ve found a way (thanks to alchymyth) not to have duplicated post within the query showing the latest posts from each category. But the posts listed in my 4 latest posts query are duplicated in the latest posts list from each category. Logical.
Now I try to find a way to avoid that. I tried to put this code before the first query (showing the 4 latest posts):
<?php $my_query = new WP_Query('posts_per_page=4');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
And close it after the query for the latest posts from each category:
<?php if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
<?php endwhile; endif; ?>
But it doesn’t work.
It may sound complicated but I’m sure I’m not the only one looking for a solution about these duplicated posts on the home page.
Thanks for your help!
I run the website https://everydaygamers.com. My problem is that my posts are duplicating on the main page. It’s kind of complicated to explain what the exact problem is but I will try…
If you visit the site you will see that I have a featured section which shows 5 posts, a latest podcasts section that shows 3 posts, a reviews section that shows 5 posts and a news section that shows 5 posts. I want to show articles from all of those categories in the “latest articles” section but I don’t want them to duplicate. Offsetting doesn’t work because it only offsets 5 posts from all categories, not specific categories.
What I’d like to do is be able to throw in some code that tells it not to duplicate any posts on the main page.
Is this possible?
If it’s not possible then is it possible to offset individual categories? I’d rather not do this but it could be a temporary fix.
Thanks for your time.
]]>