I have a while loop in my wordpress site. How to print its query details ?
while (have_posts()) : the_post();
I tried, but not working:
print_r($GLOBALS['wp_query']->request);
Add this in your functions.php then add ?debug=sql after the url, for example
http://someUrl.com/something?debug=sql
Or
http://localhost/localCopy?debug=sql
Also <?php echo $GLOBALS['wp_query']->request; ?> should work, just put it right after the loop if you didn't it before.
Related
I'm racking my brain trying to think why this isn't working. Seems to be right to me but I must be missing something.
<?php
if( have_rows('cta_box') ):
while( have_rows('cta_box') ) : the_row();
echo get_sub_field('cta_header');
echo get_sub_field('cta_content');
endwhile;
endif;
?>
Am I doing something dumb?
Thanks
It's a group field, not repeater. This'd help.
So in your case you'd use it like this.
$cta = get_field('cta_box');
echo $cta['cta_header'];
echo $cta ['cta_content];
your code is proper, i think the issue is where you want to display like in the specific template or any where in the page, you just need to assign with the template or page or post whatever its will display.
Here is the code that got it working. Can someone explain? Is the code above this ACF effecting what is returned?
<?php while ( have_posts() ) : the_post();
$cta = get_field('state_call_to_action');
echo $cta['call_to_action_heading'];
echo $cta['call_to_action_body'];
endwhile; ?>
Using WordPress 3.7.1 I am trying to display all Regular Post on my created page lest say TestPage. Here are the steps I took to do this:
1- Generate a Custom Page Template called:Test Page and loaded by following code
2- Generate a Page Called TestPage based on Test Page Template
after updating the page I am not getting any of Post on the page while I have already generated some!
<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p>Sorry, this page does not exist</p>
<?php endif; ?>
<?php get_footer(); ?>
he abouve code actually is loading the page whit title and content of the TestPage and not by Posts!Can you please let me know why this is happening?
You must understand the difference between post and page.
A page is associated with a template. Through code, you can lists posts or other pages using this page /template.
The above code will only display the Page data
You don't want to create many pages using the same tempalte in order to show different data with the same tempalte. Instead you create posts and give them a category.
Then you can fetch these posts and display them in the same way asthe test-page.
You can either use get_posts or wp_get_recent_posts.
Or, if you really want to list pages, use wp_list_pages.
Besides that, you should look at http://codex.wordpress.org/Pages and http://codex.wordpress.org/Posts
I am using a while loop
while (have_posts()) : the_post();
to get all the post and displaying a page. How to randomise the post. But the problem is, in the page I am working on there is no sign of 'query'. Also I tried to use some solutions mentioned but no result.
You can alter the main query with query_posts($args) this way:
// Alter the query
query_posts( 'orderby=RAND' );
while ( have_posts() ) : the_post();
// Do your stuff inside the loop
endwhile;
// Reset the query
wp_reset_query();
I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;
How do I list my posts in Wordpress ordered by modified date instead of created date?
I have it as
<?php $posts=query_posts($query_string . '&orderby=post_modified&order=ASC'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Having orderby=title does sort the posts by title but my above query doesn't work.
Looks like orderby=post_modified isn't recognized.
Ah ok... I figured it out.
orderby=post_modified needs to be orderby=modified.
Thanks,
Tee