我想用简码在 HTML/文本小部件中显示某些数据。
我已经在我的functions.php中进行了include php调用:
function event_widget($atts) {
// turn on output buffering to capture script output
ob_start();
// include file (contents will get saved in output buffer)
include("wp-content/themes/mytheme/widgets/event.php");
// save and return the content that has been output
$content = ob_get_clean();
return $content;
}
//register the Shortcode handler
add_shortcode('event', 'event_widget');
如果我将纯文本放入 event.php,它会将数据获取到小部件,因此调用正确完成,但我不知道如何编写 event.php 来获取此数据:
// the loop
<?php if (have_posts()) :
while (have_posts()) : the_post(); ?>
<ul>
<li>
<?php if( get_post_meta($post->ID, "customfield1", true) ): ?>
<?php echo get_post_meta($post->ID, "customfield1", true); ?>
<?php endif; ?>
</li>
<li>
<?php if( get_post_meta($post->ID, "customfield2", true) ): ?>
<?php echo get_post_meta($post->ID, "customfield2", true); ?>
<?php endif; ?>
</li>
</ul>