I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
// your output html
<?php endwhile; ?>
You have nested the loops (missing endwhile).
You cannot just invent a variable "query_string2" and expect it to work ;)
Try this:
<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:
<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
This will work:
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?
This will help me aswering more correctly.
I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)
<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page
https://codex.wordpress.org/Function_Reference/rewind_posts
Related
So I want to return a Wordpress loop that returns just one category, so currently I have something like this
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This works great and does exactly what I want it to do
But I want the category number to be set by an ACF, so I have that set up and it's returning my value just as a string on the site, all good again, so now my code looks like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('cat=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
What I want to do is something like this
<p>My category number is - <?php the_field('category_number'); ?></p>
<?php query_posts('"cat=", the_field("category_number")'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
I don't think I'm going about this the right way and can't seem to find an answer for this as I don't think I'm looking for the right thing .. if someone could point me in the right direction on this I'd be really grateful
Use get_the_field instead of the_field in query posts method something like this
query_posts('cat=get_the_field("category_number")');
note:- the_field() is echo everything which field contain.
get_the_field is only get the field not echo it.
Hope it will help you :)
Have you tried something along these line?
https://wordpress.stackexchange.com/questions/127940/use-advance-custom-field-inside-query-post-command
<?php query_posts('cat=<?php the_field("category_number"); ?>'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
Or Maybe something like this?
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I'm using a theme for some Wordpress-integrated software which means that I am basically stuck with the template hierarchy, otherwise I'd just set up a custom template to get around this.
Anyway, I have my page.php, which looks a little like the following
<?php //start the main loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile;
else:
?>
Something is missing
<?php
endif;
?>
This works fine, and as expected, I am able to add html within content-page.php
However, I would like to add a custom loop within content-page.php to display customer testimonials. I've attempted this with the code below inside content-page.php:
<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This code creates a PHP error (unexpected endwhile where I attempt to close the main loop). However, I put this exact same code straight inside page.php, it works. It only errors when inside content-page.php. Am I not able to include custom loops with get_template_part?
the last endwhile and endif are not closed
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
endwhile;
endif;
?>
I am learning WordPress theme development and I have developed a theme. All work has been done, but the author name not displaying in a blog page. I used the following:
<?php the_author(); ?>
but it is not working. Is it necessary to do any function or code in functions.php?
Basically, this is the loop of a page in wordpress:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
some wordrpess code
<?php endif; ?>
You have to enter the get request INTO the loop, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $author = get_the_author(); ?>
or
<?php the_author(); ?>
<?php endif; ?>
If not within the loop you can try this
<?php the_author_meta( 'display_name', $userID ); ?>
From Codex
I create category.php for display categories archive in a costumed template.
In a category page link like this: http://www.example.com/category/cat1/
By these codes it's OK and shows last items of cat1
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?>
But when I try to customize query by WP_Query or query_posts instead of contents of cat1 it shows contents of all categories of site
<?php query_posts( 'posts_per_page=30' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?>
What is reason and solution?
You must define cat in your query.
it's your answer:
<?php
$args = array(
'cat' => get_query_var('cat'),
'posts_per_page' => 30
);
$recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
//some template code
<?php endwhile; ?>
Here is the code:
<?php if ( have_posts() ) : ?>
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query- >the_post();
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php higher_content_nav( 'nav-below' ); ?>
<?php $wp_query = null; $wp_query = $temp;?>
<?php endif; ?>
How can I show posts per page not 5 but default? I mean post that we can set at back end at Reading Settings, Blog pages show at most - number of post.
The safest way is to call get_option, which gets the value directly from the database:
$showposts = get_option('posts_per_page');
$wp_query->query('showposts='.$showposts.'&paged='.$paged);
Global variables like $numposts are not guaranteed to be set.
For future reference, you can usually determine what to pass to get_option by finding the name attribute of the setting's <input> in WP Admin.
<?php query_posts( 'posts_per_page=5' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
You are using WP_Query(), it does not have any default value for the number of posts per page.
You should however note that showposts has been replaced by posts_per_page in your code:
$wp_query->query('showposts=5'.'&paged='.$paged);
Here is the solution:
global $numposts;
$wp_query->query( 'showposts='.$numposts.'&paged='.$paged );