wordpress短代码内容显示两次

时间:2017-04-03 10:47:21

标签: php wordpress

我正在为wordpress开发一个短代码。

使用word文件中的短代码:

add_shortcode('LATEST_NOT_ROHIT','latest_notification_rohit');
function latest_notification_rohit()
{
    include("shortcode.php");
}
shortcode.php文件中

代码如下:

<div class='alert alert-info'>Latest Notifications</div>

<?php
global $wpdb;
$select_qury = "select * from `ln_category`";
$select_cat = $wpdb->get_results($select_qury);
foreach($select_cat as $select_cat)
{
    echo "<h4>Latest Notifications For <span style='color:#800000'>".$select_cat->category."</span></h4>";
    $cat_id = $select_cat->id;

    $select_qury2 = "select * from `ln_notification` where `cat_id`='$cat_id'";
    $select_notification = $wpdb->get_results($select_qury2);
?>
<table class="responsive display table table-bordered">
<tr><th>Sr No</th><th>Organisation</th><th>Post Name</th><th>No of Post</th><th>Qualification</th><th>Fees</th><th>Adervst Date</th><th>Application Start Date</th>
<th>Application Last Date</th><th>Status</th></tr>
<?php
$i=1;
foreach($select_notification as $select_notification)
{
    $current_date = date('Y-m-d');
    $start_date = $select_notification->start_date;
    $last_date = $select_notification->last_date;
    if($current_date < $start_date)
    {
        $remark = "<span style='color:green'>Form is about to start</span>";
    }
    elseif($current_date > $last_date)
    {
        $remark ="<span style='color:red'>Last Date is over</span>";
    }
    else
    {
        $remark = "Application is going on";
    }
    echo "<tr><td>$i</td><td>".$select_notification->organisation."</td><td>".$select_notification->post_name."</td><td>".$select_notification->no_of_post.
    "</td><td>".$select_notification->qualification."</td><td>".$select_notification->fees."</td><td>".date('d-M-Y',strtotime($select_notification->adv_date))."</td><td>".date('d-M-Y',strtotime($start_date))."</td><td>".date('d-M-Y',strtotime($last_date))."</td><td>$remark</td></tr>";
    $i++;
}
?>
</table>
<?php
}
?>

但是当我在wordpress页面/帖子中运行此代码时,内容会显示两次。演示位于前端

http://singhalrohitashv.com/latest-notification/

如何解决此问题???

3 个答案:

答案 0 :(得分:0)

您可能正在调用以下某个文件上的 latest_notification_rohit()函数:

  • 页面的Header.php
  • 页面模板文件

您可能也会为该页面调用两次内容函数。对 the_content() latest_notification_rohit()进行全部搜索可能会显示多次调用代码的位置。

答案 1 :(得分:0)

public class MyDatabaseInitializer : public MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration> {}

请您尝试上面的代码吗?

答案 2 :(得分:0)

我认为问题出现在您的代码中,您使用了两个 foreach 循环。

$select_qury = "select * from `ln_category`"; 
$select_cat = $wpdb->get_results($select_qury);

可能会返回两个类别,让第一个 foreach 执行两次。就像你的表在第二个 foreach 循环之前,即使它没有记录,它也会打印第二个表。如果有记录,请把条件打印到表格。

希望这可以帮助您解决问题。

相关问题