2017-04-03 11 views
0

私はワードプレスのショートコードを開発しています。ワードファイルなどでショート使っワードプレスショートコードのコンテンツが2回表示されています

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のページ/ポストでこのコードを実行しているとき2回表示されます。デモでは、フロントエンド、私はこの問題を解決するにはどうすればよい

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

である???

+1

ファイルに 'include_once()'関数を含めるようにしてください。 – htmlbrewery

+0

not working ... –

+0

データは一度表示されますが、ヘッダーにのみ表示されます –

答えて

0

あなたはおそらく、次のいずれかのファイルにlatest_notification_rohit()関数を呼び出していますページのため

  • header.phpの
  • ページのテンプレートファイルが

またあるかもしれませんそのページのコンテンツ関数を2回呼び出します。 the_content()またはlatest_notification_rohit()のすべてを検索すると、コードが複数回呼び出されている場所が表示されることがあります。

+0

私は2回実行しているコードを見ることができない問題のコードをすべて入れました –

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

私は問題があなたのコードにあると思います、あなたは2つを使用しましたforeachループ。おそらく

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

それが2時間を実行する最初ののforeachを聞かせて2つのカテゴリを返します。そして、あなたのテーブルの直前にはforeachループがあっても、レコードがない場合でも2番目のテーブルが表示されます。レコードがあればテーブルを印刷するための条件を入れてください。

希望すると、この問題の解決に役立ちます。

関連する問題