I want to display an image "NEW" next to my post if:
It was published between 1 to 45 days ago.
Hide/disable the "NEW" image after 45 days.
Here's my code for my carousel which displays the latest 4 posts:
`<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<div class="ribbon"><div class="ribbon-new">New</div></div>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
</div>
<!----End Carousel Customizations----->`
The DIV section: <div class="ribbon"><div class="ribbon-new">New</div></div> is the container for my "NEW" image banner.
Any assistance is greatly appreciated!
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 45 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-45 days')) . "'";
return $where;
}
?>
<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
add_filter( 'posts_where', 'filter_where' );
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<div class="ribbon"><div class="ribbon-new">New</div></div>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach;
remove_filter( 'posts_where', 'filter_where' );
?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
<!----End Carousel Customizations----->`
Updated answer to display the text "New Ribbon" when the post is published in last 45 days
<!------Begin Carousel Cusomizations-------->
<div class="list_carousel">
<a id="prev2" class="prev" href="#"><</a>
<ul id="foo2">
<?php
global $post;
$args = array( 'numberposts' => -1, 'post_type' => 'guides');
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<li class="productCarousel" onclick="document.location.href='<?php echo the_permalink(); ?>';">
<div class="liContent">
<?php if( strtotime('-45 days') < strtotime( $post->post_date ) ) {
<div class="ribbon">
<div class="ribbon-new">New</div>
</div>
<?php } ?>
<div class="liPadding">
<?php the_post_thumbnail( array(75,75) ); ?>
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<p class="accessGuides">Access Guide</p>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<a id="next2" class="next" href="#">></a>
<div class="clearfix"></div>
<div id="pager2" class="pager"></div>
</div>
<!----End Carousel Customizations----->
Related
I'm trying to list the posts related to the current taxonomy page but it shows me all the posts instead. When I output the current term name is correct so really I don't know why this happens. I'm pasting here the code if anyone can put me in the right direction.
<?php
$term = get_queried_object();
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term->term_id
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Try with:
<?php
$term = get_queried_object()->term_id;
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Just use tax query.
<?php
$term = get_queried_object()->term_id;
$args = array(
'post_type' => 'properties-for-sale',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
);
$query = new WP_Query( $args ); ?>
in Wordpress, I am trying to display my most recent posts in a list. I have being able to list the links, but cant do the same with post thumbnail, title, date and category. What am I doing wrong?
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo the_date();?></p>
<h3 class="article_title"><?php echo get_the_title( $post_id ); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Another way to do it, using WP_Query
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat-a, cat-b'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php echo the_category(); ?></p>
<h3 class="article_title"><?php echo get_the_title(); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
Try below code its working for me.
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo get_the_category( $recent->ID );?></p>
<h3 class="article_title"><?php echo get_the_title( $recent->ID); ?></h3>
<p class="date"><?php echo get_the_date( $recent->ID );?></p>
</div>
<div class="mask">
<img src="<?php echo get_the_post_thumbnail_url($recent->ID,'full'); ?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
I've tried to separate search results by categories in 2 tabs. Everything works fine except showing the message "No results". This message is shown only when in both categories are nothing found. But when one tab has results and another hasn't - nothing is shown.
I'm looking for way to show "No results" for every tab. I mean, if nothing is found in cat 1 and some results found in cat 2 -> Show "No results" in Tab 1 and show results in Tab 2.
Any suggestions?
Code here:
<div id="tab-content1" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 3,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--text">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><?php the_excerpt(); ?> </span>
<span> </span>
</span>
</div>
<div class="post-item--thumbnail">
<div class="post-thumbnail">
<div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
<span></span>
<span></span>
</div>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content1 -->
<div id="tab-content2" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 4,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--circle">
<?php the_field('digest_number'); ?>
</div>
<div class="post-item--text no-margin">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><p><?php the_field('short_description'); ?></p> </span>
<span> </span>
</span>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content2 -->
</div>
You can use your if (have_posts()) : on each loop to determine if to display or not...
if (have_posts()) :
//while loop
else:
echo '<h1>no posts found</h1>';
endif;
I am displaying previews of the three most recent news articles on my homepage. The most recent post will be displayed in a different format to the second and third most recent posts.
I am currently displaying all three the same with the following code
<?php query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
How can I add another loop which will separate the second and third most recent posts from the most recent post?
I did not want to use postID as the posts will change.
The two arguments in the WP_Query function below combine to retrieve the second most recent post, then the HTML below displays that post.
<div class="video-message">
<p>
<ul>
<!-- // Define our WP Query Parameters -->
<?php
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 1 ) );
?>
<!-- // Start our WP Query -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- // Display the Post Title with Hyperlink -->
<p><?php the_title(); ?></p>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</p>
</div>
</div>
To display the third most recent post would require changing the offset value to 2, so that the program skips over the two most recent posts.
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 2 ) );
This method is discussed in the Pagination Parameters section of the WordPress Code Reference.
Untested but you could try:
<?php
$count = 0;
query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post();
if($count == 0)
{
?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
$count = 1;
}
else
{
//Some other layout here
}
endwhile;
endif;
?>
The above will check if $count is 0 and if it is, then do the layout and the count will then equal to 1. So the next time around $count won't be 0, so it will run what is in the else (which will be your layout).
I use WP_Query like this to show all posts from the fourth most recent one:
<?php
$query4 = new WP_Query( 'posts_per_page=4&offset=3' );
?>
I'm trying to display posts from a post_type in a certain way: display the first post type withing a query then in the next query to skip that first post and show the second post and so on.
I've tried to do this with offset, the first post shows but the next one doesn't show. Is there a more elegant way to achive this?
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 2);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
for starters - its healthy to use the wp_reset_postdata when you have multiple wp_queries on one page.
in terms of your question. the first post type doesnt need an offset and your second one should offset 1 (minus the first)
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'posts_per_page' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="tableCell">
<?php
$args2 = array( 'post_type' => 'service', 'offset' => 1);
$loop2 = new WP_Query( $args2 );
while ( $loop2->have_posts() ) : $loop2->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>