2017-04-03 77 views
0

我正在开发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/

我该如何解决这个问题???

+1

尝试使用'include_once()'函数包含文件。 – htmlbrewery

+0

无法正常工作... –

+0

它只显示一次数据,但仅显示在标题中 –

回答

0

你可能会调用以下文件之一的latest_notification_rohit()功能:

  • 的header.php该页
  • 页面模板文件

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

+0

我已经把所有的代码问题,我不能看到代码运行两次 –

0
add_shortcode('LATEST_NOT_ROHIT','latest_notification_rohit'); 
function latest_notification_rohit() 
{ 
    ob_start(); 
    require_once("shortcode.php"); 
    $data = ob_get_contents(); 
    ob_end_clean(); 
    return $data; 
} 

请您尝试上面的代码吗?

0

我觉得问题在于你的代码中,你已经使用了两个foreach循环。

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

可能将其返回二类这让第一的foreach执行两个时间。而作为你的表之前,第二个foreach循环,即使它没有记录它会打印第二个表。如果有记录,请将条件打印出来。

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