I used two custom loops on my site:
<?php $wp_query = new WP_Query("post_type=page&post=15"); while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php // content ?>
<?php endwhile; wp_reset_postdata(); ?>
<?php $wp_query = new WP_Query("post_type=page&post=15"); while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php // content ?>
<?php $wp_query = new WP_Query('showposts=3&post_type=contests&orderby=meta_value&meta_key=date&order=desc'); if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php //content ?>
<?php endwhile; wp_reset_postdata(); else : ?>
<?php ... ?>
<?php endif; ?>
And then I wanted to return to my regular loop for retrieving the news:
<?php rewind_posts(); ?>
<?php while(have_posts()) : <?php // content ?>
<?php endwhile; ?>
But it returns nothing (and there are posts to be displayed). When I tried to run an if(have_posts...) statement, it returned false. What I did wrong?
You're doing nothing wrong per say; WordPress is. Those loop related functions set up and reset globals, making you lose state because you're nesting WP loops. Adding insult to injury, you're overwriting $wp_query, which is at the core of the WP loop-related functions.
You could potentially work around these problems in multiple ways. One would be to use output buffers outside of the loop to set a few variables that you can use further down. Another could be to not use the API at all apart from $q = new WP_Query(...) and a normal foreach loop -- without using the built-in template functions.
(Actually learning php should trump both of these ideas if you don't understand them. Seriously, don't create a WP theme with complicated logic without knowing at least enough php to understand the source code of the template functions you're relying upon.)
Related
I used the following code in my wordpress site many time in single page
<?php $recent = new WP_Query("showposts=4&cat=186");
while ($recent->have_posts()) : $recent->the_post();
?>
<a href="<?php the_permalink(); ?>"target="_blank"><b><li>
<?php the_title(); ?>
</b></li>
</a><br>
<?php endwhile; ?>
Now my question is, what should I reset it by wp_reset_query(); or not if your answer is yes then how ?
try this code below end your loop end use WordPress function
<php wp_reset_postdata();?>
First, showpost argument is replaced by posts_per_page.
You need to reset the global $post after each custom loop if you need to access to the original $post's values (like get_the_ID(), etc ...).
The line wp_reset_query(); need to be after endwhile.
PS: You need to reset the query because you use $the_post->the_post().
I trying to loop through posts in a custom php page but no matter what I do, no posts are found
here is the code I wrote in my-custom-page.php
<?php
require_once("/wp-load.php");
get_header();?>
<div id="blog">
<?php if(have_posts()) : ?>
<?php echo"anything"; ?>
<?php endif; ?>
</div>
<?php get_footer();?>
You should require wp-load.php via the full path to this file.
Hardcoded example:
require_once("user/home/public-html/wordpress/wp-load.php");
Softcoded example (suposing your file is in the same directory as WordPress):
require_once(dirname(__FILE__)."/wp-load.php");
You have also to query the posts before you display them. So, you need to add this line to your code:
query_posts('post_type=post');
The query arguments may vary depending on what you want to display. Some of them are the member variables of the WP_Post class. Go to https://codex.wordpress.org/Class_Reference/WP_Post for reference.
Here you have a re-writing of your code that displays the titles of the 30 latest posts published:
<?php
require_once(dirname(__FILE__)."/wp-load.php");
query_posts('post_type=post&showposts=30');
get_header();?>
<div id="blog">
<?php
if (have_posts()) :
while (have_posts()) :
the_post();
the_title();
echo '<br />';
endwhile;
else :
echo 'Sorry, no posts found.';
endif;?>
</div>
<?php get_footer();
wp_count_posts :
#return object Number of posts for each status.
you're trying to echo an object which ends in a fatal error. Furthermore if you want to see all posts the_post is not right. Look for it at the function reference : https://codex.wordpress.org/Function_Reference/the_post. I would do it other (google smth like "get all posts").
if you will use the code inside your theme
use the same code of Mr.Carlos but with out dir
require_once("/wp-load.php");
I need to display the wordpress blog posts in a non wordpress php page. I have tried the following code.
<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
But it showed me the following error
Fatal error: Call to undefined function query_posts()
How to fix this?
please look into the following line in your code
require('http://test.com/wordpress/blog/wp-load.php');
in the require function you should use the relative or physical path. You should not include the url.
Since you need the wordpress database and framework this seems very unlikely to work at all. Try getting XML, RSS or JSON data from your wordpress which you can fetch with a self-made script.
For external integration, it's likely more reliable to approach this via RSS route. Simplest and possibly laziest way of making this work is by use of simplexml_load_file (and HTTP streams.)
$t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
foreach( $t->channel->item as $item ) {
printf(
"<div>%s <a href='%s'>%s</a></div><hr/>",
$item->description,
$item->link,
$item->title
);
}
This outputs the feed as you'd expect to see it. Note that this doesn't use any kind of caching, so every page request hits the original feed.
<div>Some Chinese officials are furious at Apple's iPhone for apparently
helping users have too much of a good time. Chinese media say the complaints
surround the iPhone's voice-activated personal assistant, known as
“Siri,” which has been helping some users find prostitutes and
brothels. The Mandarin language version can apparently present users with as
many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
Prostitution</a></div>
You could use the rss option, you could write a new, hidden code and read data using that file ... in JSON format
Maybe the first thing to do is search for a extention/plugin/module that does this for you;
You are not the first one who wanted to do this, i think :p
For Displaying recent posts outside wordpress setup, first include wp-load.php file.
require( './blog/wp-load.php' );
// Load the recent top 10 posts
$args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
$postslist = get_posts( $args );
Now you can loop through $postlist variable.
foreach ($postslist as $post) : setup_postdata($post);?>
<ul class="media-list">
<li class="media">
<div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
<div class="media-body">
<a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
<p><?php echo $post->post_content; ?></p>
</div>
</li>
</ul>
<?php endforeach;
After you include the wp-load.php you have to instantiate the query like this:
$wp_query = new \WP_Query();
$wp_query->query('showposts=5');
After that your loop should look like this:
<?php while ($wp_query->have_posts()) :
$wp_query->the_post();
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
I wrote this PHP code to put on a wordpress page template:
<?php
query_posts('showposts=10&cat=7');
while (have_posts()) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; ?>
When I view the page I don't see any result and the right bar of the browser continue to reduce itself. I have understand that the code create an infinite loop.
Where I mistake?
Thanks
Firstly, you should not use query_posts. It's too invasive for simple loops, and messes with the entire WP_Query. Also showposts should be posts_per_page.
Secondly, it's hard to gauge what this issue is without more context. Perhaps pastebin your entire page, and edit it into your question. My guess is a loop within a loop, and should stop at like a 100 posts. (10 X 10) but if it's reset anywhere else if could very well go infinite!
Use this code instead to create loops:
$custom_query = new WP_Query( 'posts_per_page=10' );
if($custom_query->have_posts()) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
//global $post; // for stuff like $post->post_name
// Post stuff here
// the_title();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Look at the WordPress codex for more details. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
You should use an if statement in your loop:
<?php
query_posts('showposts=10&cat=7');
if ( have_posts() ): while ( have_posts() ) : the_post();
?>
<li class="img-slider">
<?php the_content(); ?>
</li>
<?php endwhile; endif; ?>
Without further information, I would say that in each loop of the while statement, the function is returning the first row of the data? So each time the while loop executes you are calling the function again, which is returning the same row over and over again without actually iterating through the result set.
Seems like the problem with this is the PHP syntax, but no luck in Wordpress forums. This first code block generates a link to the newest post in category "posts."
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
This next code block should display the custom field data for the latest post in "posts," with the key of the custom field being "qanda." But it doesn't and it displays nothing.
<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php echo get_post_meta($post->ID, "qanda", $single = true); ?>
<?php endwhile; ?>
Thanks, Mark
try renaming your second query, otherwise Wordpress will think it is already done
<?php
$my_other_query = new WP_Query('category_name=posts&showposts=1');
while ($my_other_query->have_posts()) : $my_other_query->the_post();
echo get_post_meta($post->ID, "qanda", true);
endwhile;
?>
Apart fromthat $single = true should just be true it looks OK... try var_dump instead of echo and see what you get.
You might need to name it something different. Wordpress might think that you have already done that set of posts, so it is starting at the end, which means it doesn't have anymore posts to process.