2017-02-20 127 views
1

我想为下面的代码生成简码。我不确定在哪里放置短代码功能。我尝试了下面的代码,但短代码不起作用。 我已添加功能function lorem_function() {}。我想短代码工作,以便下面的输出可以显示在wordpress页面的任何地方。为php和html代码生成简码

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
<style type="text/css"> 
    .offer-title{ 
     font-size: 20px; 
     font-weight: bold; 
    } 
    .offer-col{ 
     height: 600px; 
    } 
    .offer-row{ 
     margin: 30px 0; 
    } 
    .offer-info{ 
     font-size: 15px; 
    } 
</style> 
<?php 
/* 
Template Name: Blank - No Header, no Footer 
*/ 
if (!defined('ABSPATH')){ die(); } 
/* 
* A blank Template that allows you to build landing pages, coming soon pages etc 
*/ 
global $avia_config; 
get_header(); 
?> 
<?php 
function lorem_function() { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://api.bstcm.no/customer/-8/offers"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, FALSE); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     $username = 'atrium', 
     $password = '3HnzkG2p', 
     curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password) 
    )); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    $details = json_decode($response, TRUE); 
    /*echo"<pre>"; 
print_r($details); 
echo"</pre>";*/ 
    $max_count = count($details[collections]); 
?> 
<div class='container_wrap container_wrap_first main_color 
<?php avia_layout_class('main'); ?>'> 
    <div class='container'> 
     <div class='offer-row col-sm-12'> 
      <?php 
    $flag=0; 
    for($i=0;$i<$max_count;$i++) 
    { 
     $count[$i] = count($details[collections][$i][offers])."...."; 
     for($j=0;$j<$count[$i];$j++) 
     { 
      $flag++; 
      if($flag%2==0) 
      { 
       if($flag%4==0) 
       { 
      ?> 
      <div class='offer-col col-sm-3'> 
       <img src="<?php echo $details[collections][0][offers][$j][offerFile][originalUrl]; ?>"><br/> 
       <span class="offer-title"> 
        <?php echo $details[collections][0][offers][$j][offerName];?></span><br/> 
       <span class="offer-info">Giltighetstid: 
        <?php echo $details[collections][0][offers][$j][startDatetime];?></span><br/> 
       <span class="offer-info">T.o.m.: 
        <?php echo $details[collections][0][offers][$j][endDatetime];?></span><br/> 
       <span class="offer-info">Begränsat antal köp per kund: 
        <?php echo $details[collections][0][offers][$j][limitedUse];?></span><br/> 
       <span class="offer-info">Antal: 
        <?php echo $details[collections][0][offers][$j][limitedTo];?> </span><br/><br/><br/> 
      </div> 
      <?php 
       } 
       else 
       { 
      ?> 
      <div class='offer-col col-sm-3'> 
       <img src="<?php echo $details[collections][0][offers][$j][offerFile][originalUrl]; ?>"><br/> 
       <span class="offer-title"> 
        <?php echo $details[collections][0][offers][$j][offerName];?></span><br/> 
       <span class="offer-info">Giltighetstid: 
        <?php echo $details[collections][0][offers][$j][startDatetime];?></span><br/> 
       <span class="offer-info">T.o.m.: 
        <?php echo $details[collections][0][offers][$j][endDatetime];?></span><br/> 
       <span class="offer-info">Begränsat antal köp per kund: 
        <?php echo $details[collections][0][offers][$j][limitedUse];?></span><br/> 
       <span class="offer-info">Antal: 
        <?php echo $details[collections][0][offers][$j][limitedTo];?> </span><br/><br/><br/> 
      </div> 
      <?php 
       } 
      } 
      else 
      { 
       if($flag%3==0) 
       { 
      ?> 
      <div class='offer-col col-sm-3'> 
       <img src="<?php echo $details[collections][0][offers][$j][offerFile][originalUrl]; ?>"><br/> 
       <span class="offer-title"> 
        <?php echo $details[collections][0][offers][$j][offerName];?></span><br/> 
       <span class="offer-info">Giltighetstid: 
        <?php echo $details[collections][0][offers][$j][startDatetime];?></span><br/> 
       <span class="offer-info">T.o.m.: 
        <?php echo $details[collections][0][offers][$j][endDatetime];?></span><br/> 
       <span class="offer-info">Begränsat antal köp per kund: 
        <?php echo $details[collections][0][offers][$j][limitedUse];?></span><br/> 
       <span class="offer-info">Antal: 
        <?php echo $details[collections][0][offers][$j][limitedTo];?> </span><br/><br/><br/> 
      </div> 
      <?php 
       } 
       else 
       { 
      ?> 
      <div class='offer-col col-sm-3'> 
       <img src="<?php echo $details[collections][0][offers][$j][offerFile][originalUrl]; ?>"><br/> 
       <span class="offer-title"> 
        <?php echo $details[collections][0][offers][$j][offerName];?></span><br/> 
       <span class="offer-info">Giltighetstid: 
        <?php echo $details[collections][0][offers][$j][startDatetime];?></span><br/> 
       <span class="offer-info">T.o.m.: 
        <?php echo $details[collections][0][offers][$j][endDatetime];?></span><br/> 
       <span class="offer-info">Begränsat antal köp per kund: 
        <?php echo $details[collections][0][offers][$j][limitedUse];?></span><br/> 
       <span class="offer-info">Antal: 
        <?php echo $details[collections][0][offers][$j][limitedTo];?> </span><br/><br/><br/> 
      </div> 
      <?php 
       } 
      } 
     } 
    } 
    return $details; 
      ?> 
     </div> 
    </div> 
    <?php get_footer(); 
} 
add_shortcode('lorem', 'lorem_function'); 
    ?> 

回答

0

您需要将输出绑定到php wrap并返回输出以显示它在WordPress中的任何位置。

例,

$a = '<div></div>'; 
$a .= '<div class="2"></div>'; 
$a .= 'continue your code'; 
return $a; 
+0

我的代码包含html和php代码。我如何创建一个变量来保存html div – Rashmi