ดึง category, author, date มาแสดงผลใน wordpress

โค้ดชุดนี้เอาไปใช้บ่อยใน wordpress เวลาต้องการแสดงข้อมูลของเนื้อหา เช่น category, author, date ด้วยความที่ง่อยเปลี้ยด้าน php ก็เลยคิดว่าบันทึกไว้สักหน่อยน่าจะดี โดยเฉพาะตรง author นั้นบันทึกไว้อย่างมีนัยสำคัญว่า มันเป็นการดึง first name และ last name มาแสดงผล ไม่ใช่ดึงชื่อ username นะไอ้ทิด


 

<div class=”article-info”>

หมวดหมู่:&nbsp;<?php
$categories = get_the_category();
$separator = ‘ ‘;
$output = ”;
if($categories){
foreach($categories as $category) {
$output .= ‘<a href=”‘.get_category_link( $category->term_id ).'” title=”‘ . esc_attr( sprintf( __( “View all posts in %s” ), $category->name ) ) . ‘”>’.$category->cat_name.'</a>’.$separator;
}
echo trim($output, $separator);
}
?>

ผู้เขียน:&nbsp;<a href=”<?php echo esc_url( get_author_posts_url( get_the_author_meta( ‘ID’ ) ) ); ?>” rel=”author”><?php
$fname = get_the_author_meta(‘first_name’);
$lname = get_the_author_meta(‘last_name’);
$full_name = ”;

if( empty($fname)){
$full_name = $lname;
} elseif( empty( $lname )){
$full_name = $fname;
} else {
//both first name and last name are present
$full_name = “{$fname} {$lname}”;
}

echo $full_name;
?></a>

เผยแพร่เมื่อ:&nbsp;<?php echo get_the_date(‘F j, Y’) ; ?>

</div>

เป็นนักเขียนเพียงคนเดียวของเว็บไซต์นี้ (แหงละ) เป็นมนุษย์ที่ชอบบันทึกเรื่องราวเอาไว้ เนื่องจากไม่ถนัดในการจดใส่กระดาษ จึงมักเขียนไว้ในโลกออนไลน์ บางเรื่องผิด บางเรื่องถูก แต่บันทึกความทรงจำจะช่วยตักเตือนเรา