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
Related
I am really a newbie in Wordpress and php. I created a theme for my website as follows:
I think my question is clear: in wordpress admin page I have four categories: 1,2,3,4. I want last 2 posts in category 1 to be displayed in section (div) 1 (shown above), last 2 posts in category 2 to be displayed in section (div) 2, and so on.
As I said, I am novice and I faced a tons of functions in Wordpress documentation.
for example I used the code below inside index.php (of my custom theme) in section 2 which outputs: "Sorry, no posts matched your criteria."
<?php
$args = array( 'post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها');
$the_query = new WP_Query( $args );
?>
<?php
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
?>
Please give a piece of code to do this.
This should work:
$posts = get_posts ("cat=1&showposts=2");
if ($posts)
{
foreach ($posts as $post):
setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php endforeach;
}
Change cat=1 to the id of each of the four categories.
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;?>
Hope someone can help me, I've been struggling for days on this trying to find the answer...
Basically, I have a wordpress site that has a slider (not a plug-in just open source code) which is called to using a 'get_template' but it displays the same three posts on every single page. I have different posts in different categories and want the slider to correspond on each separate page and echo the posts from each particular category.
<div id="slidorion">
<div id="slider">
<?php
query_posts( 'posts_per_page=3' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="slide">"><?php the_post_thumbnail(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div id="accordion">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="link-header"><?php the_title(); ?></div>
<div class="link-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
here is a link to the site if you need to see it to totally understand what I mean and need to do...
http://www.kaijocreative.co.uk/footballnatter
Thanks!
You should alter your query adding cat or category to your query_posts( 'posts_per_page=3' ); according to what you exactly want
see Query_posts () and also have a look at WP_Query class
you need to use find out the category ids from each post, then use these ids in the
$category = get_the_category();
$post_catid= $category[0]->term_id;
$querystr='cat='.$post_catid.'&posts_per_page=3';
query_posts($querystr);
I have a custom template I am using for my page and in that custom template I get the_content() then I use a custom query. I want to be able to only get 6 of my posts then be able to go to prev and next to see the rest of the posts.
Here is some of my code:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<div id="the_content">
<?php the_content(); ?>
</div>
<?php
$args = array('numberposts'=> 6,
'post_type'=>'project'
);
$posts = get_posts( $args );
if($posts){
foreach( $posts as $post ) : setup_postdata($post);
//do stuff
wp_reset_postdata();
endforeach;
endwhile;
</div>
</article>
?>
Try this
<?php posts_nav_link();
Reference: http://codex.wordpress.org/Next_and_Previous_Links
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();
?>