最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to show categories and date on posts

programmeradmin1浏览0评论

I want to categorise posts and show them on my WordPress website. So in order to achieve that I've written a code. My code is below.

function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 
    'category_name' => 'travel', 
    'posts_per_page' => 5 
) ); 
   
// The Loop
if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            if ( has_post_thumbnail() ) {
            $string .= '<li>';
            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
            } else { 
            // if no featured image is found
            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
            }
            }
    } else {
    // no posts found
 $string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
   
return $string;
   
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');

This code works perfectly fine. However, I want to show posts meta descriptions as well (i.e post created date and the category). How would I achieve that? Right now It only extracts the post heading and the image. Thanks so much for helping me out.

I want to categorise posts and show them on my WordPress website. So in order to achieve that I've written a code. My code is below.

function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 
    'category_name' => 'travel', 
    'posts_per_page' => 5 
) ); 
   
// The Loop
if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            if ( has_post_thumbnail() ) {
            $string .= '<li>';
            $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
            } else { 
            // if no featured image is found
            $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
            }
            }
    } else {
    // no posts found
 $string .= '<li>No Posts Found</li>';
}
$string .= '</ul>';
   
return $string;
   
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');

This code works perfectly fine. However, I want to show posts meta descriptions as well (i.e post created date and the category). How would I achieve that? Right now It only extracts the post heading and the image. Thanks so much for helping me out.

Share Improve this question asked Feb 28, 2022 at 19:48 Havishka HalangodaHavishka Halangoda 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Something like this for the category term should work. That's for the slug you probably want the name.

<?php $terms = get_the_terms( $post->ID, 'category' ); ?>
<?php foreach( $terms as $term ) echo ',' . $term->slug; ?>
 

The date is pretty simple just follow the documentation
https://developer.wordpress.org/reference/functions/get_the_date/ it has lots of examples at the bottom.

发布评论

评论列表(0)

  1. 暂无评论