display latest post with thumbnail wordpress - php

I would like to display the 3 latest posts of my Wordpress blog using a shortcode. I wrote this php code. It works but display 3 times each posts. Do you know how to fix it please ?
$counter = 3;
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
$recent_posts = wp_get_recent_posts(3);
while ($recentPosts->have_posts()) : $recentPosts->the_post();
foreach( $recent_posts as $recent ){
echo '<li class="box'.$counter--.'">';
the_post_thumbnail();
echo '' . $recent["post_title"].'';
echo '</li>';
}
endwhile;

Please using wordpress default function wp_get_recent_posts use this code
<h2>Latest Posts</h2>
<ul>
<?php
$args = array(
'numberposts' => 3,
);
$recent_posts = wp_get_recent_posts($args, $output = ARRAY_A);
foreach( $recent_posts as $recent ){
echo get_the_post_thumbnail( $recent["ID"], 'thumbnail' );
echo '<li>' . $recent["post_title"].' </li> ';
}
?>
</ul>
Also adjust your code where you want display.

Please try below code.
<?php
$postslist = get_posts('numberposts=3&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?>
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>

Please try this code.
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
?>

Related

Trying to fetch a posts sub category in Wordpress theme

On my wordpress site, I have a section only displaying a lidt of post excerpts from within a specific parent category. Each post has a single sub-category which I'd like to also display alongside the title and excerpt.
At the moment I'm able to get and siaply the required posts but each post is displaying every subcategory, even ones not assigned to it. How can I modify this to show only assigned subcategories?
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$args2 = array('child_of' => 1);
$categories = get_categories( $args2 );
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
EDIT:
With Growdzens help I ended up with this that worked.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p>
<span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
$cat1 = 4;
$cat2 = $category->cat_ID;
if( $cat2 != 1 ){
echo $the_sub;
}
}
?>
</span>
</p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Wordpress has a function for getting all categories of a post on which you can read more here: get_the_category()
This function can be used in the loop like the example below to get the desired results.
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
foreach($categories as $category) {
$the_sub = $category->name;
echo $the_sub;
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>
Update:
To check if a category is a child of a specific other category you can use the Wordpress function cat_is_ancestor_of()
<div class="blog-items">
<?php
$args = array( 'posts_per_page' => 8, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<div class="blue">
<p><span>
<?php
$categories = get_the_category( $post->ID);
$parent_id = 4; //change this ID to the parent ID you want to show the subcategories for
foreach($categories as $category) {
$the_sub = $category->name;
if(cat_is_ancestor_of( $parent_id, $category->cat_ID )){
echo $the_sub;
}
} ?>
</span></p>
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(__('(more…)')); ?></p>
read more
</div>
<?php endforeach;
wp_reset_postdata();?>
</div>

wordpress get recent posts with description

I use Wordpress and I want to get recent posts with thumbnail and description.
So I use wp_get_recent_posts.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_excerpt().'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
I can get post title and link, but I can't get description:
<p>'.get_the_excerpt().'</p>
You have just missing of post_id in description without post_id it can't display anything.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_content($recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
echo wpautop( $recent['post_content'] );

How to retrieve the title of the latest post in Wordpress?

I am working on a website that is based on Wordpress. My client wants to display the latest post snippet under the header on the main page. I was able to do that but instead of fetching the post's title in the loop it is fetching the page title and displaying it there.
Here is my code :
<?php
$postslist = get_posts('numberposts=1&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?><?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have also tried get_the_title(); but it did not work as well.
The SITE ITSELF
From wordpress codex
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
$recent_posts = wp_get_recent_posts(array('numberposts' => 1);
foreach( $recent_posts as $recent )
echo $recent["post_title"];
if you want to use the loop
$args = array('posts_per_page' => 1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?> <?php the_excerpt(); ?>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please use this:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><?php echo $recent['post_title'] ?></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>
It worked. There is a plugin called smart post lists that was not letting it work. Now it is working fine.
It was my mistake to not disable it and test this. My code is correct.

if category empty return a message

i have this code to get some elements from wordpress
global $post;
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php $i++; endforeach; ?>
i need to put a condition to return text message if category is empty.like no posts to display , please note that the post use WPML as the language switcher
<?php
$myposts = get_posts( $args );
if($myposts){
//found posts
}else{
//no posts
}
?>
UPDATE: PLEASE check if the code works probably and THEN compare it with your code, i have commented on the changes i made, so its your chance to learn:
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
//check if $myposts
if(!$myposts){
//the $myposts has no posts, print the error message
echo "<li>";
echo "This category has zero posts";
echo "</li>";
}else{
//the category has one more or more posts
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php
$i++; endforeach;
}
?>
Thank you so much!! This solved my problem..
ref: howlingwolfmedia.com/site3/classes/class-categories
see 'Personalized Training'
my code here:
`
<?php
$args=array(
'child_of' => 4,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<h3>' . $category->name . '</h3>
<div>';
global $post;
$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
//alternatively this also works: 'nopaging' => true
$myposts = get_posts( $args );
if (!$myposts) {
echo 'Sorry there are currently no classes in this category';
}
else {
foreach( $myposts as $post ) : setup_postdata($post);
echo '<p>' . get_the_title($post->ID) . '</p>';
endforeach;
}
echo '</div>
';
}
?>
`

Get posts all posts by Tag / URL Wordpress

I'm looking to create a tag.php page, which displays all tags when the user clicks on the tag from a tag cloud. This is what I've got so far, but this seems to display all of my posts.
<article class="articles">
<?php
echo '<h2>Tag:';
$tag = single_tag_title();
echo '</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(''); ?>
<?php endforeach;?>
</div>
I cannot seem to figure this out, I've been googling it but I can't seem to find out the information I want...
Your single_tag_title it's not returning to variable:
$tag = single_tag_title('', false);
try with:
<?php
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(); ?>
</div>
<?php endforeach;?>
Try using wp_query
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
// The Query
$the_query = new WP_Query( $tag );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post = get_queried_object();
echo "<div class='clear'></div>
<span class='timestamp'>" . mysql2date('j M Y', $post->post_date) . " </span></h2>
<p class='about'" . the_title() . "</p>";
<?php the_content(); ?>
</div>
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please note, I haven't tested this code!

Categories