Multiple Loops / WP_Query on One Page - php

I'm attempting to have two loops on my archive-custom.php (it's for a custom post type) - one loop for featured post(s) and another for the rest of the posts.
This is the code I have come up with, however, it's not working correctly. At the moment, it doesn't display either loop and ends up actually breaking other PHP based elements.
Note: These loops are split up into different template parts - not sure if that matters or not. However, I have combined them into one chunk to make it easier to troubleshoot.
<?php $args = array (
'post_type' => 'community',
'category_name' => 'featured',);
// The Query
$community_posts_featured = new WP_Query( $args );
if ($community_posts_featured->have_posts()) : while ($community_posts_featured->have_posts()) : $community_posts_featured->the_post(); ?>
<div id="featured">
<--Featured Stuff Here-->
<?php the_content(); ?>
</div><!--End #featured-->
<?php endwhile; ?>
<?php $args = array (
'post_type' => 'community', );
// The Query
$community_posts = new WP_Query( $args );
if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
<div id="main-content">
<--Main Stuff Here-->
<?php the_content(); ?>
</div><!--#End Main-->
<?php endwhile; ?>
<?php else : ?>
<--Missing Content Stuff-->
<?php endif; ?>

There i can spot two problems:
1) You have opened 2 if statements and have just closed one of them
2) you'd better use wp_reset_query(); after the first loop

Related

Seprate sticky posts from main loop. #WordPress

I need your kind help. I want to seperate sticky posts from main loop (tempate : index.php) and want to add something after sticky posts before rest of loop. Please see the screenshot. Any tip for this custom loop? I'll be gratful.
Screenshot : http://imgur.com/a/e37Pj
Structure is something like :
— These are sticky posts
– sticky post one
– sticky post two
– sticky post three
++ Add something after sticky posts.
— The rest of main loop.
– normal post one
– normal post two.
– normal post three.
Thanks in advance.
Regards
– Pomy
Before standard loop in index you create your area create place where you want to load that "sticky" posts
in post page write this
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 'Sticky' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
This will get you posts from category you want, in this case you will get 5 posts from "sticky" category
Try this:
<?php
$args = array('posts_per_page' => 12, 'post__in' => get_option('sticky_posts'));
$stickyposts = new WP_Query($args);
while($stickyposts->have_posts()) : $stickyposts->the_post();
?>
<!-- sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- -->
<!-- here you can add random text or a horizontal line with a new headline or s.th.
<!-- like that. This is the exactly place between sticky and normal posts -->
<!-- -->
<?php
$args = array('posts_per_page' => 12, 'post__not_in' => get_option('sticky_posts'));
$normalposts = new WP_Query($args);
while($normalposts->have_posts()) : $normalposts->the_post();
?>
<!-- normal posts without sticky posts (max. 12) -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
This will show 12 sticky posts on top of your page. After that you can enter some random text, or s.th. like that, followed by 12 "normal" posts, that are not sticky posts.
Try using an incrementing variable (ie. $counter++) along with a conditional statement within the loop to trigger what you want after looping pass all(3) sticky posts.
<?php
$count=0; //Count is 0 outside of loop
while ( have_posts() ) : the_post(); // Begin WP loop
if(!is_sticky()) : $count++; endif; // Start count for nonsticky loops
if(!is_sticky() && $count==1){
// Do stuff when the first none-sticky post is discovered inside the loop
// ie. Add a div, or insert a class name, etc.
}
// Here is an example of an actual implementation
if(!is_sticky() && $count==1): echo '<div class="not-sticky">'; endif;
// Loop countinues
endwhile; // Loop ends
if(!is_sticky(): echo '</div><!-- .not-sticky -->'; // Placed outside of loop or add inside a conditional statement within the loop to only run once
?>
...Years later, but I hope it may still help someone else who might be facing the same issue.

Fix for wordpress loop

I am new too wordpress and have one problem. I finaly understand post loop and it work well but have one problem. For example: I have two pages of my posts (all are 6). In one page i have five posts (excerpt only) and on secound page i have one post only and in this case (when on one of my pages i have only one post it change to content post but it need to stay excerpt). So my question is what can i change in my code to make it work well in this case when on some page left only one post?
It's my loop:
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 1) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for excerpts-->
<?php the_excerpt() ?>
<!-- Do your post footer stuff here for excerpts-->
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for single post-->
<?php the_content() ?>
<!-- Do your post footer stuff here for single post-->
<?php endwhile; ?>
<?php endif; ?>
<?php else : ?>
<!-- Stuff to do if there are no posts-->
<?php endif; ?>
Use the posts_per_page parameter to an array and start the loop.
$args = array( 'post_type' => 'post', 'posts_per_page' => 4,'category' => 2, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$content = get_the_content();
echo $content;
endwhile;
Here I have printed the 4 contents from the archive. So the page will only have 4 posts in it. You can change the value based on your need.
I have used and got the output. Hope this helps you too.
Ur second if condition should be inside while and while shuld be called once ..
Somthing like
If
While
Then if for count
Elseif
Else
End while
End if
I hope it wil help ..
Answer for all: $wp_query->post_count need to be replaced by $wp_query->found_posts and everything working fine now!

wordpress php loop - archive is looping the same number of times as results

Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>

Need help using different 'page-templates' for one-page wordpress site sections

This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>

Random Wordpress posts outside of the main loop without duplicate posts. How?

Well I can't figure this one out...
I have this Wordpress I use as a photo gallery blog.
I have a basic setup using the main default loop for posts.
Like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; ?>
<b>Not Found</b>
<?php endif; ?>
In the sidebar and where ever, I want to appear random posts.
I've managed to do that. With this:
<?php query_posts($query_string . 'showposts=1&orderby=rand'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; endif; ?>
It looks amazing! In theory.
There are duplicate posts all over the place. And that just looks stupid.
I have read lots of articles but I just can't seem to get it to work :(
Any help would be much appreciated.
Try this code for random post.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Or You can get help from this url mention below
http://codex.wordpress.org/Template_Tags/get_posts
After a good night of sleep, here's what I have done:
Creating array with post ID:
<?php $already_posted = array(); ?>
The Main loop where at the end I record the post ID to array:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php $already_posted[]= $post->ID; endwhile; ?>
<?php else : ?>
<b>Not Found</b>
<?php endif; ?>
And the random post code using post__not_in to avoid duplicates and again recording post ID:
<?php $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => $already_posted );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
//the post
<?php $already_posted[]= $post->ID; endforeach; ?>
Works evertime!
You can do amazing stuff with this :)
Thanks to paislee and Arvind Pal for helping out.
Skip would-be duplicates by remembering displayed ID's from the first loop
$displayed = array(); // create an array that we'll use associatively
In your first loop, each time:
$displayed[get_the_ID()] = TRUE; // <-- save all post IDs in here
Change your random loop opening like this:
<?php if (have_posts()) : while (have_posts()) : the_post();
// skip post IDs you've already seen
if ($displayed[get_the_ID()]) continue;
?>
Due to randomness in the number of duplicates, you may want to alter your query so that it gets all posts, and change the second loop to break once the desired number of random posts is reached.
Notes
showposts is depracated. Replace showposts=1 with posts_per_page=-1

Categories