簡體   English   中英

生成php和html代碼的短代碼

[英]generate shortcode for php and html code

我想為以下代碼生成短代碼。 我不知道在哪里放置短代碼功能。 我嘗試下面的代碼,但短代碼不起作用。 我添加了函數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');
    ?> 

您需要將輸出綁定到php wrap並返回輸出以在WordPress中的任何位置顯示它。

例,

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

將shortcode函數始終放在function.php中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM