Wordpress Multi Loop Conditional Conflict - wordpress

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

Related

wp_reset_query() or wp_reset_postdata()

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.

WordPress Author Page with posts separated by category

Creating my WordPress author.php template page and ran into a little roadblock. I want to be able to separate all the Author's posts by the parent category they are in. For example, I have these major categories of posts: 'Books', 'Audio', 'Video', 'Curriculum'. On the author page, I want those overarching categories to be the header of each section and then the posts related to that section listed under the header.
Is the only way to do it to run multiple loops or is there a more efficient way?
Thanks!
Allan
You need to keep the file name author.php so you get that slug and link, but remove the standard WP loop and use a new query for each category, like this:
<?php $my_query = new WP_Query('category_name=mycategoryname&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; ?>
This can be used multiple times in a page template without conflict. Change showposts=-1 to 1 for one post, 10 for ten posts, etc. -1 shows all.
Use html to arrange the output of those loops into columns.

Wordpress grid/list view

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

how to select posts by particular author? (wordpress)

I have a page with template No Sidebars I want to list 5 posts' titles on that page by author where the author's name = page's title
any idea how to do so without using any plugin?
I thought that query_posts function would do the trick but this important note kind of tells me that I cannot use query_posts
Here's a bit of code that will probably get you the Post Titles by author; in order to have this automatically feed off of the page title I'd have your title generation code... Hope this helps you get at least part of the way there...
<?php $my_query = new WP_Query('author_name=YourAuthor&showposts=5'); ?>
<?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; ?>
Check out Function Reference/WP Query « WordPress Codex and an earlier answer (with the same code :) as poindexter's) at Wordpress display featured posts outside of loop

Display Wordpress Archives one category at a time?

I am almost done with my humble attempt at a custom CMS using Wordpress. The only difficulty I have is making a page display the archive for only one category (and it's children). Anyone has an idea?
Thanks a lot!
Regis
You can create your own custom archive page using the class WP_Query. Specifically, something like:
<?php $query = new WP_Query('category_name=code'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<!-- display the category here -->
<?php endwhile; ?>
You can look at the default theme's archive.php to get a feel for what else is needed to display a particular category in a layout you are familiar with.
This is what I use to show a list of titles and permalinks for the category "mycategory"; this can go inside the main Wordpress loop:
<?php $my_query = new WP_Query('category_name=mycategory&showposts=-1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><?php the_title(); ?><?php endwhile; ?>
Change the "-1" to "1" to show only the one most recent post; to "10" to show the last 10 posts, etc.

Resources