生成php和html代码的短代码

时间:2017-02-20 07:17:19

标签: php html wordpress

我想为以下代码生成短代码。我不知道在哪里放置短代码功能。我尝试下面的代码,但短代码不起作用。 我添加了函数function lorem_function() {}。我希望短代码lorem工作,以便下面的输出可以显示在wordpress页面的任何地方。

<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');
    ?> 

2 个答案:

答案 0 :(得分:0)

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

实施例,

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

答案 1 :(得分:0)

将您的短代码功能始终放在function.php中。