ฉันจะรับเนื้อหาของโพสต์ด้วยรหัสโพสต์ได้อย่างไร ฉันพยายามget_page('ID');
แสดงเนื้อหา แต่ใช้งานไม่ได้
ฉันจะรับเนื้อหาของโพสต์ด้วยรหัสโพสต์ได้อย่างไร ฉันพยายามget_page('ID');
แสดงเนื้อหา แต่ใช้งานไม่ได้
คำตอบ:
คุณสามารถทำได้หลายวิธี ต่อไปนี้เป็นวิธีที่ดีที่สุดสองวิธี
$post_id = 5// example post id
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo do_shortcode( $content );//executing shortcodes
วิธีอื่น
$content = get_post_field('post_content', $post_id);
echo do_shortcode( $content );//executing shortcodes
หลังจากที่ปีเตอร์ Goosen apply_filters
ข้อเสนอแนะเกี่ยวกับ
คุณสามารถใช้apply_filters
หากคุณต้องการให้เนื้อหาถูกกรองโดยปลั๊กอินอื่น ๆ ดังนั้นจึงไม่จำเป็นต้องใช้do_shortcode
ตัวอย่าง
$post_id = 5// example post id
$post_content = get_post($post_id);
$content = $post_content->post_content;
echo apply_filters('the_content',$content);
//no need to use do_shortcode, but content might be filtered by other plugins.
หากคุณไม่ต้องการที่จะอนุญาตให้ใช้ปลั๊กอินอื่น ๆ do_shortcode
เพื่อกรองเนื้อหาและความจำเป็นในรหัสนี้ฟังก์ชั่นแล้วไปกับ
post_content
หากคุณไม่ต้องการรหัสเกินไปแล้วเพียงแค่เล่นกับ
do_shortcode
raw content
โพสต์ รหัสย่อใด ๆ ที่ฝังในโพสต์จะไม่ถูกประมวลผล ดังนั้นเราจึงทำเช่นนั้นด้วยตัวเราเองด้วยdo_shortcode
apply_filters( 'the_content', $content );
วิธีนี้ฟิลเตอร์ทั้งหมดที่จะนำไปใช้the_content()
เช่นและจัดการรหัสที่ถูกนำไปใช้wpautop
$content
;-) สังเกตพหูพจน์filters
apply_filters
แทนdo_shortcode
สมเหตุสมผล แต่การใช้apply_filter
นั้นขึ้นอยู่กับการตัดสินใจของสภาพแวดล้อม ให้ฉันปรับปรุงคำตอบของฉันด้วย ขอบคุณมากสำหรับการดูแลชุมชน @PieterGoosen
ฉันจะออกจากที่นี่อีกวิธีที่น่าเกลียดที่คุณอาจพบว่ามีประโยชน์ในบางครั้ง วิธีการเรียนการสอนที่ใช้การเรียก API เป็นที่ต้องการเสมอ (get_post (), get_the_content (), ... )
global $wpdb;
$post_id = 123; // fill in your desired post ID
$post_content_raw = $wpdb->get_var(
$wpdb->prepare(
"select post_content from $wpdb->posts where ID = %d",
$post_id
)
);
$id = 23; // add the ID of the page where the zero is
$p = get_page($id);
$t = $p->post_title;
echo '<h3>'.apply_filters('post_title', $t).'</h3>'; // the title is here wrapped with h3
echo apply_filters('the_content', $p->post_content);
get_page('ID')
โดยการใช้
$page_id = 123; //Page ID
$page_data = get_page($page_id);
$title = $page_data->post_title;
$content = $page_data->post_content;
get_page()
ถูกตัดค่าเสื่อมราคา
get_page()
ในขณะที่คุณไม่ได้พยายามที่จะอ่านเอกสารบน มันถูกคัดค้านเมื่อนานมาแล้ว นอกจากนี้ยังมีทรัพยากรไม่ จำกัด จำนวนในเว็บไซต์เกี่ยวกับปัญหานี้แม้ google มีข้อมูลมากมายในเรื่องนี้