I have multiple loops that I want to display on my WordPress blog. The categories are as follows:
Top-story (1 per page)
Small-story (1 per page)
Normal (1 per page)
Quickfill (2 per page)
I've got 4 loops to display these. I'm trying to display this amount of posts per page and once they've been displayed, don't show them again on another page, as I don't want to show duplicates.
My loops are as follows. I seem to be getting duplicates and when I'm trying to get them to remove duplicates I tend to remove everything without knowing how.
Top story - 1st loop
<?php
global $do_not_duplicate;
$do_not_duplicate = array();
$paged = max(1, get_query_var('paged'));
$my_query = new WP_Query('category_name=top-story&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
// content
<?php endwhile; ?>
Small-story - 2nd loop
<?php
$my_query = new WP_Query('category_name=small-story&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID;
?>
// content
<?php endwhile; ?>
Normal posts (note: the category is actually called normal) - 3rd loop
<?php $my_query = new WP_Query('category_name=normal&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID; ?>
// content
<?php endwhile; ?>
Quickfill - 4th loop
<?php
$int = 0;
$my_query = new WP_Query('category_name=quickfill&posts_per_page=2&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID;
if ($int==0) { ?>
<hr class="seperator" />
<div class="gr_bg_post">
<img src="<?php bloginfo('template_directory'); ?>/images/quickfill.png" />
<?php } ?>
<div class="fl">
<h4><?php the_title(); ?></h4>
<?php the_content('<button type="button" class="read_more_green">Read More</button>'); ?>
</div>
<?php if ($int==1) { ?>
</div>
<?php } ?>
<?php $int++; ?>
<?php endwhile; ?>
Last loop - standard WordPress protocol for display posts... (I think this may have something to do with my duplicates)
<?php
$my_query = new WP_Query(array('post_not_in' => $do_not_duplicate));
if (have_posts()) : while ($my_query>have_posts()) : $my_query>the_post();
?>
// content
<?php endwhile; endif; ?>
I've been trying to figure this out for a few days now and I just can't seem to stop duplicates showing, or get the correct posts to show. Any help is greatly appreciated!
You are missing the query variable $my_query in the last query
$my_query = new WP_Query(array('post_not_in' => $do_not_duplicate));
if ($my_query->have_posts()) : while ($my_query>have_posts()) : $my_query>the_post();
// content
endwhile; endif;
Related
I have my own custom WordPress theme and I am building out the 'Latest News' section at the moment. I have a custom field for my page header image and alt tag for each page on my site, I am using the following code to display those custom fields on each page:
<section class="page-heading">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endwhile; endif; ?>
</section>
Which works fine. But on my 'Latest News' page all the custom fields from all the news stories (posts) are being displayed, so for example if I have 3 post displayed per page then I am getting 3 images and 3 alt tag displayed.
In my settings I have my 'Posts Page' set to 'Latest News'.
Is there a way I can only have 1 image and 1 alt tag displayed and not all 3?
Thanks.
EDIT
I've added an image to better explain... The images that span the screen are from the news stories and not the custom field for the 'Latest News' page.
I don't know if I understand your question, but you want your first post show the image and the rest not?
I guess you have to write something like this:
<section class="page-heading">
<php $show_first_image = true; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if($show_first_image): $show_first_image = false; ?>
<img src="<?php the_field( 'header_image' ); ?>" alt="<?php the_field( 'main_header_image_alt_tag' ); ?>">
<?php endif; ?>
<?php endwhile; endif; ?>
I think I understand your problem, your latest news is also a Page (post type), which has the header image.
Your index file/archive is like this:
<?php
get_header();
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
the problem is, got its items loaded before the get_header(), where your header image will be showed.
What you do is something like this:
<?php
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$page = get_query_var( 'pagename' );
$wp_query = new WP_Query( array('post_type' => 'page', 'pagename' => $page ) );
get_header(); // in the get_header() you will probably have the <section class="page-heading">
$wp_query = null;
$wp_query = $original_query;
if(have_posts()):
while(have_posts()):
// your latest news * 3.
endwhile;endif;
get_footer(); ?>
I hope this is your answer to your question.
I got around this by creating a new template for my news page and using WP_Query inside that template to bring in the latest news stories. I also unset 'Posts Page' in my reading settings as that's what was causing all the issues.
I'm not sure if that is the most efficient way of doing things but it worked for me.
VVC, I appreciate your help and effort.
<!-- WP Query Arguments -->
<?php
$args = array(
'page_id' => '103'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
<!-- WP Query Arguments -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '5'
);
$the_query = new WP_Query( $args );
?>
<!-- End WP Query Arguments -->
<!-- WP Query loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="news-story">
<div class="news-heading">
<h5><?php the_title(); ?></h5>
<p class="publish-date"><strong>Posted on:</strong> <?php the_time('j M, Y'); ?></p>
</div>
<?php the_excerpt(); ?>
<p>Full Story <i class="fa fa-angle-double-right"></i></p>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
<!-- End WP Query loop -->
Y try paginate results of wordpress and use this script , the problem it´s when i go paginate , the number of pages show right but if go the links of paginate always show the same content no change in each number of pages
<?php
global $post;
if (have_posts()) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
while (have_posts()) : the_post(); ?>
<div class="cols_posts">
<?php
$imagen = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$ruta_imagen = $imagen[0];
echo '<img src="'.$ruta_imagen.'">';
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="extracto"><?php the_excerpt(); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
In tye page number one must show the content for that page , in the page 2 the same , etc but always show the same page , the first page content
Regards
The problem is here:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
You are getting $paged variable correctly but you are not using it in a query.
So add it to the query like this:
query_posts( 'cat=36&posts_per_page=4&paged='.$paged );
You can use a function for this. For that, visit this page
http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/
I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>
s it possible to retrieve post entry for a custom post type by tag, I have been trying with the following code, however it just locks me into a infinte loop.
<aside class="supporting_images">
<?php /*<?php if($post->ID == 241) : echo apply_filters('the_content', '[slideshow=3]'); endif; ?>
<?php the_post_thumbnail(); ?>*/?>
<?php if($post->ID == 241) : ?>
<?php
$query = new WP_Query();
$query->query('tag=branding');
?>
<?php while ($query->have_posts()) : ?>
hello
<?php endwhile; ?>
<?php endif;?>
First try changing your loop to:
<?php while ($query->have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
The have_posts() function just returns true if there is another post but does not increment the loop counter, so the loop will never end.
I'm having hard time trying to use teasers post in my wordpress theme (based on 960gs), as you can see here http://img17.imageshack.us/img17/794/schermata20110420a15045.png what I got till now is one "featured" post and three teasers post with thumbnails that will probably be six (so it'll have seven posts displaied in the homepage). The problem is that to do so I have to assign a class "grid_2 alpha" to the teasers post and I don't know how to assign this class to just the first teaser on the left, lefting the other ones with no alpha or omega class and putting the omega class to just the last teaser post (the seventh).
If can help, here's the code I'm using for the loop:
<?php $firstClass = 'firstpost'; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $firstClass; ?>">
<?php $firstClass = 'grid_2 alpha'; ?>
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" width="140" height="100" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>
also I would like to know how I can add some text above the teaser section under the first featured post. Sorry for the too many questions and for my bad english, as you can understand I'm not a developer but I searched for one week and couldn't find anything helpful for my problems. Thanks in advance for any help, I really appreciate it.
<?php $count = 0; ?>
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1):
$class = "firstpost';
elseif ($count == 2):
$class = "grid_2 alpha";
elseif ($count == $wp_query->post_count):
$class = "grid_2 omega";
else:
$class = "grid_2";
endif;
?>
<?php if (function_exists('yoast_breadcrumb')) { if (is_page() && $post->post_parent) { yoast_breadcrumb('<p id="breadcrumbs">','</p>'); } } ?>
<div class="post <?php echo $class; ?>">
<img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" style="padding-bottom:20px;" />
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'roots'), 'after' => '</p></nav>' )); ?>
</div>
<?php endwhile; // End the loop ?>