I have some code written which displays the latest 5 posts from a specific category however I can't work out how to make it display the latest 5 posts from that category that are marked as featured only. By featured I mean the post has been stickied, so basically it will display the 5 posts from each category that have been stickied.
<ul id="index-blog">
<?php $the_query = new WP_Query( 'category_name=whats-on&showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<li>
<?php the_excerpt(__('(more…)')); ?>
</li>
<?php endwhile;?>
</ul>
Try this:
$sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args);
You can get the top 5 sticky posts using rsort and array_slice as shown in http://codex.wordpress.org/Sticky_Posts
The issue with the other answer is that introduces a variable - $category - that you have to first populate.
Here's the revised code, including how to populate the variable:
<ul id="index-blog">
<?php $category_id = get_cat_ID( 'whats-on' );
$args = array(
'cat' => $category_id,
'post__in' => get_option('sticky_posts'),
);
?>
<?php $the_query = new WP_Query($args); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Fix some markup issues here - the children of `ul` elements must be `li` elements....
<li>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<div class="excerpt">
<?php the_excerpt(__('(more…)')); ?>
</div>
</li>
<?php endwhile;?>
</ul>
Related
I created a separate page for blog posts on my website (WordPress). I Used this code for showing 6 last posts:
<div class="container">
<div class="entry-content">
<div class="last-posts">
<?php $the_query = new WP_Query( 'posts_per_page=6' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-md-4">
<div class="post-item">
<a href="<?php the_permalink() ?>" target="_blank">
<figure><?php the_post_thumbnail(); ?></figure>
</a>
<div class="post-detail">
<h3>
<a href="<?php the_permalink() ?>" target="_blank">
<span><?php the_title(); ?></span>
</a>
</h3>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
The problem is, this loop works from "container" class to the end of the code, not just 6 last posts. This loads all 6 posts 6 times.
HI you are making a small mistake in the WP_Query
please try the following line.
<?php $the_query = new WP_Query( 'posts_per_page' => 6 ); ?>
I hope this will work for you.
$args = array('posts_per_page' =>6, 'post_type' => 'post','order'=>'desc','post_status'=>'publish');
$the_query = new WP_Query( $args );
I am working on a services cpt and would like to display a list of other posts, within the same cpt on the single post.
The code I am using is:
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
Now my question is, is there a way to add a class to the current post? The intention being to style it different to the other posts in the list.
This is very easy you can always add the class in before and after
Read this documentation https://developer.wordpress.org/reference/functions/the_title/
<?php the_title( '<div class="wrapper">', '</div>' ); ?>
Yes First get current post id and match that id with loop id and if it is same than just add class whereever you want just use below code
<?php
$current_post_id = get_the_ID();
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6, ));
?>
<ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
$class = ($current_post_id == $loop->ID) ? "current-post" : '';
?>
<li class="<?php echo $class; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
It will add class "current-post" to only current post and in other posts it will add nothing.
style using
<style>
li{
/*all posts*/
}
li.current-post{
/*specific for the current post*/
}
</style>
Using function
<?php post_class(); ?>
E.g:
<div <?php post_class(); ?>>
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
I've done my hour+ research to find any answer but cannot find a solution.
Anyways... I have a wordpress posts page and on individual posts I have a query to display the latest 4 posts at the bottom of each post page.
What I'd like to do is only show 4 posts that are older than the current post page a visitor is on. Currently it shows the latest 4 entries no matter which post you're viewing.
Thank you.
<ul>
<?php $the_query = new WP_Query( array (
'post__not_in' => array( $post->ID ),
'posts_per_page' => 4,
'ignore_sticky_posts' => 1
) ); ?>
<div class="row">
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li class="col-xs-12 col-md-3">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();
?>">
<?php the_post_thumbnail( 'post-thumbnail', ['class' => 'img-responsive
responsive--full'] ); ?>
</a>
<?php endif; ?>
<p><?php the_title(); ?></p>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</ul>
You can use get_previous_post() to get the previous post.
use this in a loop (assuming $post variable is updated for every post) to get previous 4 posts.
I want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.