如何在WP模板中调用插件中的函数?

Ste*_*ven 6 wordpress wordpress-plugin

我已经创建了一个日历插件,现在我想在我的一个模板中显示一个事件列表.我现在使用的代码是这样的:

include_once(WP_CAL_PLUGIN_DIR.'eventcal.class.php');

$calendar = new EventCalendar();
$events = $calendar->getMultipleEvents('5');

(...)

<table>
<?php foreach($events as $event) : ?>
  <tr>
    <td><span><?php echo $calendar->formatEventTime($event->startTime,'dm'); ?></span></td>
    <td><span><?php echo $calendar->formatEventTime($event->startTime,'time'); ?></span></td>
    <td><?php echo $event->name; ?></td>
  </tr>
<?php endforeach; ?>
</table>
Run Code Online (Sandbox Code Playgroud)

有没有办法在我的插件中调用函数而不必包含WP插件并创建新的类实例?

Joh*_*och 8

要在模板中执行短代码,请使用该功能do_shortcode('[my-shortcode-handle]').您的短代码需要像正常一样注册(请参阅短代码API上的WordPress codex),然后才能在模板中使用它.任何属性,内部内容等也应该在那里.

echo do_shortcode( '[my-shortcode foo="bar"]Shortcode content[/my-shortcode]' );
Run Code Online (Sandbox Code Playgroud)

另外,请记住回显返回(或至少将其分配给变量),因为它只返回短代码的输出.