[WordPress] ดึง Widget มาแสดงใน Template ด้วย PHP ง่าย ๆ

ไม่เคยรู้มาก่อนว่าเราสามารถดึง Widget ที่อยู่ใน Appearance > Widgets ต่าง ๆ มาแสดงใน Template ได้เลย โดยไม่ต้องสร้าง Sidebar มารองรับ

WordPress เค้ามีคำสั่ง the_widget มาให้เรา ซึ่งสามารถเอาไปโยนใส่ใน Template File ได้เลย แล้วเลือกเอาว่าจะแสดง Widget ไหน เช่น

<?php the_widget( 'WP_Widget_Calendar' ); ?>

หรือ

<?php the_widget( 'WP_Widget_Categories', $instance, $args ); ?>

สามารถส่ง Argument ต่าง ๆ เข้าไปได้ เหมือนกับลาก Widget มาวางจริง ๆ เลย สะดวกมาก ๆ

Reference: https://codex.wordpress.org/Function_Reference/the_widget


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *