การเลือกแสดงโพสต์ใน WordPress ตามประเภท (Post Type) หรือหมวดหมู่ (Category) สามารถทำได้หลายวิธี โดยใช้ WordPress Query และฟังก์ชันที่เกี่ยวข้อง เช่น WP_Query
, query_posts
, หรือ get_posts
โดยต่อไปนี้คือรายละเอียดวิธีการเลือกแสดงโพสต์ตามประเภท:
1. ใช้ WP_Query
เพื่อเลือกโพสต์
WP_Query
เป็นวิธีที่ยืดหยุ่นที่สุดในการดึงโพสต์ที่ตรงกับเงื่อนไขต่าง ๆ เช่น Post Type, หมวดหมู่, หรือแท็ก
ตัวอย่าง: แสดงโพสต์ตามประเภท (Post Type)
$args = array(
'post_type' => 'custom_post_type', // ระบุประเภทโพสต์
'posts_per_page' => 5, // จำนวนโพสต์ที่จะแสดง
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title('<h2>', '</h2>');
the_content();
endwhile;
wp_reset_postdata(); // รีเซ็ตข้อมูลโพสต์หลังจากใช้ WP_Query
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
ตัวอย่าง: แสดงโพสต์ตามหมวดหมู่ (Category)
$args = array(
'category_name' => 'news', // ระบุ slug ของหมวดหมู่
'posts_per_page' => 5,
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title('<h2>', '</h2>');
the_content();
endwhile;
wp_reset_postdata();
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
ตัวอย่าง: แสดงโพสต์หลายประเภทพร้อมกัน
$args = array(
'post_type' => array('post', 'custom_post_type'), // ระบุหลายประเภท
'posts_per_page' => 10,
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title('<h2>', '</h2>');
the_excerpt(); // แสดงเนื้อหาแบบย่อ
endwhile;
wp_reset_postdata();
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
2. ใช้ get_posts สำหรับการดึงโพสต์ง่าย ๆ get_posts ใช้สำหรับดึงโพสต์อย่างรวดเร็วในรูปแบบของอาร์เรย์ ตัวอย่าง: ดึงโพสต์ตามประเภท (Post Type)
$args = array(
'post_type' => 'custom_post_type',
'numberposts' => 5, // จำนวนโพสต์ที่จะแสดง
);
$posts = get_posts($args);
if ( $posts ) :
foreach ( $posts as $post ) :
setup_postdata($post);
the_title('<h2>', '</h2>');
the_excerpt();
endforeach;
wp_reset_postdata();
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
3. การใช้ query_posts (ไม่แนะนำ) แม้ query_posts จะสามารถเลือกโพสต์ได้ แต่ไม่แนะนำเนื่องจากอาจทำให้เกิดปัญหาด้านประสิทธิภาพ
query_posts(array(
'post_type' => 'custom_post_type',
'posts_per_page' => 5,
));
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
the_title('<h2>', '</h2>');
the_content();
endwhile;
wp_reset_query(); // รีเซ็ต query หลังการใช้งาน
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
4. การเลือกโพสต์โดยใช้ Taxonomy หากคุณต้องการเลือกโพสต์ตาม Custom Taxonomy หรือ Tag: ตัวอย่าง: ดึงโพสต์ตาม Custom Taxonomy
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy', // ระบุ taxonomy
'field' => 'slug',
'terms' => 'specific-term', // ระบุ slug ของ term
),
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
the_title('<h2>', '</h2>');
the_excerpt();
endwhile;
wp_reset_postdata();
else :
echo 'ไม่มีโพสต์ที่จะแสดง';
endif;
5. การใช้งานใน Template File สำหรับ หน้า Archive ของ Custom Post Type: สร้างไฟล์ archive-{post_type}.php สำหรับ หมวดหมู่เฉพาะ: สร้างไฟล์ category-{slug}.php คำแนะนำเพิ่มเติม Performance: ใช้ WP_Query แทน query_posts เพื่อหลีกเลี่ยงปัญหาด้านประสิทธิภาพ Pagination: หากแสดงโพสต์จำนวนมาก ใช้ paginate_links() หรือ the_posts_pagination() เพื่อจัดการการแบ่งหน้า Custom Styling: ใช้ CSS และ Template Part (get_template_part) เพื่อปรับแต่งการแสดงผล