我正在制作我的第一个仪表板小部件,它正在成功显示,但下面函数的输出HTML显示在仪表板页面上打开HTML标记之前。
涉及的代码如下。在add\\u action()调用之前是否需要一些init调用?
function example_add_dashboard_widgets() {
wp_add_dashboard_widget(
\'donationsmtcarmel\', // Widget slug.
\'Mt. Carmel Donations\', // Title.
\'draw_widget\' // Display function.
);
}
add_action( \'wp_dashboard_setup\', \'draw_widget\' );
function draw_widget() {
$dObj = new Donation;
$donations = $dObj->retrieve("`paypal_txn` != \'\' AND `archived` = \'0\' ORDER BY `date` DESC");
echo \'
<div id="normal-sortables-20" class="meta-box-sortables ui-sortable"><div id="donations_postbox" class="postbox" style="margin-top:12px">
<div class="handlediv" title="Click to toggle"><br></div><h3 class="hndle"><span style="padding-left:12px">Mt. Carmel Donations</span></h3>
<div class="inside">
<div class="table table_content" style="padding-left:12px">\';
echo "<p><strong>Recent Donations:</strong></p><p style=\'padding-left:12px\'>";
if ($donations) {
foreach ($donations as $d) {
echo $d->donor_name.\' $\'.$d->tx()->mc_gross.\' \'.date(\'n/j/Y g:i A\', strtotime($d->date))."<br/>\\n";
}
} else {
echo \'None.\';
}
echo \' </p>
<p><a href="http://www.foo.org/wp-admin/index.php?page=donations">Donations Page</a></p>
</div>
</div>
</div>
</div>
\';
}
最合适的回答,由SO网友:Milo 整理而成
你的add_action
应该调用注册仪表板小部件的函数,然后仪表板小部件又调用该函数来呈现小部件。现在你有add_action
调用函数直接呈现小部件。
更改:
add_action( \'wp_dashboard_setup\', \'draw_widget\' );
收件人:
add_action( \'wp_dashboard_setup\', \'example_add_dashboard_widgets\' );