I'm getting confused with this so I ask with an example:
<ul class="prod">
<?php $woop = new WP_Query($wol);
while ( $woop->have_posts() ) : $woop->the_post(); global $prod; ?>
<h3><?php the_title(); ?></h3>
<?php woocommerce_template_loop_add_to_cart( $woop->post, $prod );?>
<?php endwhile; wp_reset_postdata();?>
</ul>
In this example should I use wp_reset_query() or wp_reset_postdata()?
What I understand is that I had to use wp_reset_postdata() after every custom WP_Query() and wp_reset_query() after every loop using query_posts().
Is that simple as that?
You should never have to use wp_reset_query(), which is only used to restore $wp_query and global post data to the original main query when using query_posts() (which you should never use).
Instead, you should only be using wp_reset_postdata() when you want to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query(). You have used this correctly in your example.
Related
I want to include the content of an existing page in the header.php file? What's the easiest way to do this? I am guessing that there is a way to load an existing page via a php call, however I am not sure of the correct syntax.
Thanks, in advance.
You could instantiate a new query:
$query = new WP_Query( 'page_id=7' );
After doing so, you make a loop to display the query's content.
<?php if ( $query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); // Important, so this loop does not affect the global post object afterwards ?>
<?php endif; ?>
This is based on WP's official codex: https://codex.wordpress.org/Class_Reference/WP_Query
I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!
Does anyone know what the best way to implement a list/grid togglable view in wordpress would be?
To be more clear: the default view shows the posts in a list, I would like to have the option to show them in a grid.
I have so far created a loop which shows only the thumbnails, and included it in another template.
But I don't know how I would link to that view. Would I best off using an archive view?
Thanks.
One of the easiest solutions is to create a page template containing a grid view and add a link to this page in the list view. Very simple but just what you need.
I had the same issue with pagination. WordPress has built in function posts_nav_link that automatically prints links to previous and next pages if needed. The problem is that this function works only with $wp_query instance of WP_Query (this instance is used by default, for example to get page's content or the latest posts in home). So the solution for you:
<?php
$temp=$wp_query;
$wp_query=null;
$wp_query = new WP_Query('showposts=4');
while(have_posts() ) : the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile;
posts_nav_link();
$wp_query = $temp; ?>
I think that's quite obvious and there is no need of explanation :) should be working
Thanks dude. I kind of got it working for now with this:
<?php $latest = new WP_Query('showposts=4'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I think my mistake was trying to get it to use get_template_part( 'loop', 'grid' ); It would show the page, but no posts. Even if used get_template_part( 'loop', 'index' );
I'll need to figure out the pagination, but I'm putting it down for now to work on something else.
Thanks for your help so far! #Gediminas
Alright I have two loops going, on in the body and on in the sidebar. I also have a conditional statement in the footer that generates another loop. The problem I'm running into is the use of the conditional statement in the footer. Because the loop in the sidebar was called last, Wordpress is using its variables in the conditional statement in the footer, and causing it to return false.
maybe there's some way to make some of the variables in loop in the body $_GLOBAL, that way I can use it later on and not have it conflict with the loop in the sidebar?
thanks
Run a new query; you can run as many as you want within the standard WP loop. I don't what you're doing in your current loops, but this is an example of a new query that can coexist with the main WP loop as well as other instances of the query:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
Also see Function Reference/WP Query « WordPress Codex and Function Reference/wp reset query « WordPress Codex
Doing something like this:
<?php $my_query = new WP_Query('category_name=process&posts_per_page=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
Destroys the original reference to the single post, which I would want to list afterward. What is the best way to resolve this? I have tried both of the multiple loop examples on the wordpress doc page, and either I misunderstood them, or they don't work. A friend suggested that the only way to do it was to store the original post ID and then to call that post by ID afterward. If that's the solution, then I could use:
<?php $thePostID = $post->ID; ?>
To get the post id, but how do I query a single post by id?
Thanks in advance.
To query single post by ID you can use get_post( $post_id );, for usage check http://codex.wordpress.org/Function_Reference/get_post