Wordpress - Multiple Post Queries - php

I am using the following code to query posts for categories:
<?php query_posts("cat=8"); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article>
<h4><?php the_title(); ?> </h4>
<p><?php the_content(); ?></p>
</article>
<?php endwhile; ?>
It seems to work fine, until I did it a third time(three instances of the code above) on a single page. Now the page seems to load forever and it breaks as if it is compiling more then 1 page template. I should mention that all works fine unless I publish a post to the third category
Has anyone had a problem like this, or know why it happens?
Is this bad practise for querying posts?

Use WP_query instead so you can make use of the wp_reset_postdata which should clear up the issue.
<?php
$the_query = new WP_Query( 'cat=8' );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<article>
<h4><?php the_title(); ?> </h4>
<p><?php the_content(); ?></p>
</article>
<?php
endwhile;
wp_reset_postdata();
?>

Related

Wordpress taxonomy.php loop only showing 2 posts?

I'm having some real trouble with the below code in my taxonomy.php template in Wordpress. The query is working (i.e pulling posts only from within that custom taxonomy) but it is only displaying 2 posts (4 are in the taxonomy).
All my efforts to convert this into a standard loop using $args simply result in posts from all taxonomies being pulled into the page. I was hoping it is as simple as adding posts_per_page => -1 but this just causes every post in the entire site to display.
As I understand it from the codex, taxonomy pages should pull the relevent posts by default, without a need for a loop?
Any help much appreciated!
taxonomy.php
<?php get_header(); ?>
<main>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
UPDATE
<main>
<?php
$args = array(
'posts_per_page' => -1
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<figure>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<figcaption>
<h4><?php the_title(); ?></h4>
<h5><?php the_excerpt(); ?></h5>
</figcaption>
</figure>
<?php endwhile; ?>
<?php endif; ?>
</main>
<?php get_footer(); ?>
If you have 6 different taxonomies then there will be 6 different template files to show those taxonomies appropriately. In your case your templates will be taxonomy-topics.php taxonomy-places.php taxonomy-dates.php taxonomy-interviewee.php taxonomy-period.php and taxonomy-a-z.php
In this way once these templates are created your templates will be showing appropriate posts. To achieve that you can use posts_per_page argument or you can visit this page for better understanding about fetching posts WP_Query Codex Page Hope that makes sense by now

WordPress Advanced Custom Fields Relationship Single Page

I have a custom post type for "Clubs" and custom post type for "Events by Clubs". How can I get the name of the club on my single event page (not the while loop)?
I have searched and searched, but all I can find is to get it in a loop.
Below is the code for the single event page.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: { get Club Name from Club post type relationship } </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>
You need to set up the postdata for the $club post object in order to access it:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$club = get_field('club_field_name');
if($club):
$post = $club;
setup_postdata($post);
$club_name = get_the_title();
wp_reset_postdata();
endif; ?>
<h1><?php the_title(); ?></h1>
<section class="entry-content">
<ul>
<li>Club Name: <?php echo $club_name; ?> </li>
<li>Other detail: <?php the_field('blah_blah'); ?>
<li>.....other details.....</li>
</ul>
<?php the_content(); ?>
</section>
<?php endwhile; endif; ?>
A couple of points:
You must remember to call the wp_reset_postdata(); function after you call setup_postdata($post); as otherwise other bits of the page won't work (as you're overriding the main page loop)
If you want to link to the club page you can move my piece of code to within the <li> tag and then use the_permalink(); as usual to output a link to the page (I've done it outside of your code to keep things clear).
Give me a shout if you're still stuck.

Wordpress Making ONLY most recent post display on front-page.php

I am having trouble figuring out how to make sure that only my most recent post is displayed on the front page of my front-page.php file, since I have some custom post types and would like for my most recent post to just display below my most recent of the custom post type.
Sorry if this makes no sense, basically I just want to know what to add to the wordpress loop to make sure that it only gives me the most recent post, and no others.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="section_title">
<h2>Recent Update:</h2>
</div>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<p>Read More</p>
</div>
<div class="front_page_image">
<img src="images/annoyed_victoria.jpg">
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
You can use the pre_get_posts hook to limit posts_per_page to 1.
Add the following to your functions.php.
function my_main_query($query){
if( !is_admin() && $query->is_main_query() ){
$query->set( 'posts_per_page', 1 );
}
}
add_action('pre_get_posts','my_main_query');
Thank to Nathan Dawson for pointing out the flaws of query_posts().
See the Wordpress Codex for more information on the loop and how to alter it.
Try this, it should fetch the most recent 5 posts.
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(moreā€¦)')); ?></p>
<span class="visit">See post</span>
<?php endwhile;?>

Display posts that belong to certain category - Wordpress

I'm having trouble getting this to work. Can someone provide a quick snippet for category template that displays posts that belong to a category called 'Product A'. I've been using the trial and error method for the past 3 hours with no luck.
Thank you!
Here's what I've been playing around with -
<?php
/*
Template Name: yadayada
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php query_posts('cat=32&showposts=5'); ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
You can used the WP_Query class.
One way I've done it before is by first creating a category name of Product-A and making the slug 'product-a' all lower case.
Then instantiate a new instance of the class. Pass in the parameter of 'category_name=product-a' You do no pass in the category name with this parameter, but rather the slug name. once you do that you should be able to use the WP_Query as follows:
<?php $my_query = new WP_Query( 'category_name=product-a' ); ?>
<?php if ($my_query->have_posts() ) : ?>
<?php while ( $my_query->have_posts()) : $my_query->the_post() ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="product-excerpt"><?php the_content(); ?> </div>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>
pretty much everything is the same as the regular loop but instead of just
<?php if(have_post()) : while(have_post()) : the_post() ?>
You would used object notation to refer to this particular query.
<?php if($my_query->have_post()) : while($my_query->have_post()) : $my_query->the_post() ?>
hope it helps.
First get your Product A category id; (if you use, your cat id in your custom query it 's gonna work perfectly instead of category name.)
<?php
query_posts('cat=1');
while (have_posts()) : the_post();
the_content();
endwhile;
?>

Is my loop done wrong - WordPress

Last time I checked I knew how to write a loop to display all my posts.....How ever when I wrote this:
if (have_posts()){
while (have_posts()): the_post();
?>
<div clas="span6">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php
endwhile;
}
I ran into an issue:
If I have the following posts:
Post1, Post2, Post3
Posts 1 - 2 will show up in a list, its only until I write a new post (Post 4) and publish it that post 3 will show up in that list.
So whats wrong with my loop?
never had this issue before.
Note: WordPress 3.5 is being used.
I have checked WordPress Docs to make sure I am doing things right and as far as I know I am.
I see a couple things that could be causing your problems. First, you have a set of { } that are not needed. Your open to the loop should be:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Instead of:
<?php if (have_posts()){ while (have_posts()): the_post(); ?>
See the curly brackets in your loop?
Then as McNab said, just fix your div class by adding the "s" and remove the echo from content pullers.
You also need to to include an endif; after your endwhile;.
So your full loop should loop like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="span6">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; endif; ?>
I think this should correct your problem.
You can use it like below. This is mainly used in wordpress.
if (have_posts()) : // your code if (have_posts()){
while (have_posts()): the_post();
?>
<div clas="span6">
<h3><?php echo the_title(); ?></h3>
<p><?php echo the_excerpt(); ?></p>
</div>
<?php
endwhile;
endif; //your code here }
Hope it may work.

Categories