Wordpress Making ONLY most recent post display on front-page.php - 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;?>

Related

Wordpress posts displaying the same content

I have a simple custom page template with a simple loop that displays links to posts of the same category ID=11.
However, the problem is that although the links are working correctly, all posts are displaying the same content (the content of the first post). I can't work out why this is. Any help would be really appreciated, thanks.
Here is the loop on the custom page template
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
else:
// no posts.
endif;
?>
And here is what I have on single.php
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
You should call the_post() in single.php before doing anything else.
Try this:
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
Leave your other code alone. It looks like it should be working as expected.
Worked it out through some trial and error. I had a list of post titles in the sidebar and needed to use wp_reset_query.
In single.php use the following code to get the content and title.
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
use this in your custom page, i used wp_reset_postdata();
<?php
$args = array('cat' => 11);
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else:
// no posts.
endif;
?>
And on single.php use this
<?php
while ( have_posts() ) : the_post();
the_title(); // For post title
the_content(); //For post content
endwhile;
?>

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 category.php not sorting by category when using query_posts

I'm using the SuperSimple Theme for a wordpress blog and am trying to customize the category.php page. I want to have a big image for the latest post in each category on top of a smaller grid of all the older posts.
So far I have it working the way I want, except the top image (div id="post1") is just the most recent post overall instead of the latest post for the category. Here's one of the category pages: http://meanmargie.com/category/hospitality/
And here's the code I have:
<header class="header">
<h1 class="entry-title"><?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<div id="post1">
<?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<br>
<div id="post2">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; ?>
</div>
First of all, return the category ID of the category page you are on with get_query_var('cat')
$category = get_query_var('cat');
$cat_id = $category->cat_ID;
Now, return that id back into WP_Query, using 'cat'=> $cat_id,. Please note, showposts is depreciated, use posts_per_page instead
DO NOT USE query_posts. You should never ever use query_posts. It breaks and alters the main query and fails outright in most cases with pagination.
Here's the solution I ended up using:
<div id="post1">
<?php //new query to limit number of posts
$wp_query = new WP_Query();
$wp_query->query($query_string."&posts_per_page=1&paged=".$paged);
?>
<?php WP_Query; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div class="caption"><?php the_title(); ?></div><div class="excerpt"> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>

Wordpress - Calling on Posts From Different Categories Through a Slider

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);

Wordpress - Multiple Post Queries

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();
?>

Categories