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.
Related
I have a related posts slider but currently there is not enough posts to actually slide. The design is such that it's not a simple case of displaying the posts but not sliding them until there is more.
As a short term solution, I am trying to find a way to loop through the posts twice to give the appearance of an infinite loop.
I am using slick slider and initially tried the settings:
infinite: true,
loop: true
but I can't get it to work even though apparently that should do the trick.
I am now trying to just pull through the posts twice
I have tried adjusting the count to things like
$count+1;
$count = 2
$count+=5;
All sorts of variations but I think I am way off base.
I would appreciate any help or a point in the right documentation. I have been reading about iterations but I can't grasp how they would be included in this as I had help with this from another developer.
<div class="log-book-carousel">
<?php
$current_page_id = get_the_ID();
$args = [
'posts_per_page' => '6',
'post__not_in' => [$current_page_id]
];
$the_query = new WP_Query( $args ); ?>
<?php
// Start our WP Query
while ($the_query -> have_posts()) : $the_query -> the_post();
// Display the Post Title with Hyperlink
?>
<div class="slides match-height">
<a href="<?php the_permalink(); ?>" title="Read more of the blog post '<?php the_title_attribute(); ?>'">
<?php if (has_post_thumbnail()) : ?>
<div class="log-book-slider-image">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="log-book-details-wrapper white-bg">
<h3 class="black log-title"><?php the_title(); ?></h3>
<div class="log-book-slider-excerpt ">
<p class="log-book-text"><?php sew_display_post_intro_block( $post->ID, 10 ); ?></p>
</div>
</div>
</a>
</div>
<?php
$count++;
endwhile;
wp_reset_postdata();
?>
</div>
I have a working '$related = get_posts' masonry on a single.php page. I also added a hover overlay, so that when the user hovers on the thumbnail, a transparent overlay appears as well as the descriptions (title name, category and nickname).
The problem I am facing is that when I hover on one related post thumbnail, the overlay appears for every post (the overlay is stretched, it is not appearing individually). I've also attempted to call out the descriptions, but it's only calling the current post I am viewing (e.x. the current single.php header says Snow, when I hover the first related post, it also calls out the description for Snow), however, if you click on the first related post thumbnail, it takes you to a different article (it is not calling the description for the different article).
<div class="related">
<h3>Related</h3>
<div class="js-masonry">
<div class="overlay">
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'orderby' => 'rand', 'numberposts' => 3, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) { setup_postdata($post); ?>
<?php the_post_thumbnail(array(300,300)); ?>
<?php } wp_reset_postdata(); ?>
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
</div>
</div>
</div>
As the title says, how do I pull the correct description and overlay for one post via hover for ' $related = get_posts' on the single.php page in WordPress?
I resolved the issue by reorganizing the code correctly.
<div class="js-masonry">
<?php $args = array(
'category__in' => wp_get_post_categories( get_queried_object_id() ),
'posts_per_page' => 3,
'orderby' => 'rand',
'post__not_in' => array( get_queried_object_id() )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="item-masonry overlay">
<a href="<?php the_permalink();?>">
<div class="posts">
<?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';}?>
<h1><?php the_title();?></h1>
————
<h4><?php the_author();?></h4>
</div>
<?php the_post_thumbnail(array(300,300)); ?>
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>
I am working on a site and I have a custom post type and that post type is featured to the home page. When you click read more you get "this webpage has a redirect loop" This only happens on this post. Why is this happening?
You can see it here. http://taabc.org/
If you go down to Alumni Spotlight and click read more. That is where I get the error.
Please help!
<?php
$args = array(
'post_type' => 'alumni spotlight',
'posts_per_page' => '1',
);
$the_query = new WP_Query( $args ); ?>
<div class="alumni-spotlight-bg">
<div id="container" class="alumni-spotlight">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2>Alumni Spotlight</h2>
<div class="alumni-name"><?php the_title(); ?></div>
<div class="alumni-spotlight-video"><?php the_field( 'youtube_video' ); ?></div>
<p><?php the_field( 'excerpt' ); ?></p>
<a class="alumni-spotlight-btn" href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/core/images/buttons/read-more-btn.png" /></a>
<?php endwhile; else: ?>
<p>There are no posts or pages here.</p>
<?php endif; ?>
</div>
</div><!-- End alumni-spotlight --->
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>