Wordpress custom loop in template part - wordpress

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;
?>

Related

Post not displaying in wordpress theme

I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

How to get all posts from one category in Wordpress

How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>

costumed query in wordpress category.php

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; ?>

wordpress wp query posts per page default

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 );

WordPress Multiple Loops

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

Resources