wordpress shorcode php进入html输出

时间:2017-09-26 08:52:39

标签: php wordpress return output shortcode

我有这段代码:

// Add Shortcode Team
function team_code() {
$output = "";
$output .= "<div id='b-team-all'>";
$output .= "<div class='header-team'>";
$output .= "<span class='b-icon'><i class='fa fa-users' aria- hidden='true'></i></span>";
$output .= "<p></p>";
$output .= "<h2>Náš tím</h2>";
$output .= "</div>";
$output .= "<div id='b-full-grid'>";
$output .= "<div id='b-team'>";
$output .= "<?php foreach( get_cfc_meta( 'tim' ) as $key => $value ){ ?>";
$output .= "<div class='b-team-member' style='background-image: url('<?php  the_cfc_field( 'tim','fotka', false, $key ); ?>');'>";
$output .= "<div class='b-team-text'>";
$output .= "<h1><?php the_cfc_field( 'tim','meno', false, $key );  ?></h1>";
$output .= "<divider></divider>";
$output .= "<span><?php the_cfc_field( 'tim','postavenie', false, $key ); ?></span>";
$output .= "</div>";
$output .= "<div class='b-team-overlay'>";
$output .= "</div>";
$output .= "</div>";
$output .= "<?php } ?>";
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";

return $output;

}
add_shortcode( 'team', 'team_code' );

如何,写这个PHP的权利?现在以这种方式获取PHP不起作用,我不是很好在这:)可以有人帮忙吗? 感谢

4 个答案:

答案 0 :(得分:0)

你可能会做这样的事情,只是将所有PHP内容放在输出之外:

 function team_code() {
$output = "";
$output .= "<div id='b-team-all'>";
$output .= "<div class='header-team'>";
$output .= "<span class='b-icon'><i class='fa fa-users' aria- hidden='true'></i></span>";
$output .= "<p></p>";
$output .= "<h2>Náš tím</h2>";
$output .= "</div>";
$output .= "<div id='b-full-grid'>";
$output .= "<div id='b-team'>";
foreach( get_cfc_meta( 'tim' ) as $key => $value ){
$output .= "<div class='b-team-member' style='background-image: url('".the_cfc_field( 'tim','fotka', false, $key );"');'>";
$output .= "<div class='b-team-text'>";
$output .= "<h1>".the_cfc_field( 'tim','meno', false, $key )."</h1>";
$output .= "<divider></divider>";
$output .= "<span>".the_cfc_field( 'tim','postavenie', false, $key )."</span>";
$output .= "</div>";
$output .= "<div class='b-team-overlay'>";
$output .= "</div>";
$output .= "</div>";
}
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";

return $output;

}
add_shortcode( 'team', 'team_code' );

答案 1 :(得分:0)

这是一些可怕的代码! :-P但是!这就是你想要的。

例如更改:

$output .= "<span><?php the_cfc_field( 'tim','postavenie', false, $key ); ?></span>";

对此:

$output .= "<span>".the_cfc_field( 'tim','postavenie', false, $key )."</span>";

对于您的foreach,请执行以下操作。变化:

$output .= "<?php foreach( get_cfc_meta( 'tim' ) as $key => $value ){ ?>";

对此:

foreach( get_cfc_meta( 'tim' ) as $key => $value ){ 

这一行:

$output .= "<?php } ?>";

只是

}

然后它将循环播放,您可以继续添加到$output

答案 2 :(得分:0)

 function team_code(){
<div id='b-team-all'>
<div class='header-team'>
    <span class='b-icon'><i class='fa fa-users' aria- hidden='true'></i></span>
        <p></p>
    <h2>Náš tím</h2>
</div>
<div id='b-full-grid'>
    <div id='b-team'>
        <?php foreach( get_cfc_meta( 'tim' ) as $key => $value ){ ?>
        <div class='b-team-member' style='background-image: url('<?php  the_cfc_field( 'tim','fotka', false, $key ); ?>');'>
            <div class='b-team-text'>
                <h1><?php the_cfc_field( 'tim','meno', false, $key );  ?></h1>
            <divider></divider>
            <span><?php the_cfc_field( 'tim','postavenie', false, $key ); ?></span>
            </div>
                <div class='b-team-overlay'>
            </div>
        </div>
        <?php } ?>
    </div>
</div>
</div>
}
 add_shortcode('team','team_code');

如果发现真的很难,为什么要对代码进行讽刺

答案 3 :(得分:0)

解决了我的结果,是我的坏事:

// Add Shortcode Team
function team_code() {

$output = "";
$output .= "<div id='b-team-all'>";
$output .= "<div class='header-team'>";
$output .= "<span class='b-icon'><i class='fa fa-users' aria- hidden='true'></i></span>";
$output .= "<p></p>";
$output .= "<h2>Náš tím</h2>";
$output .= "</div>";
$output .= "<div id='b-full-grid'>";
$output .= "<div id='b-team'>";
foreach( get_cfc_meta( 'tim' ) as $key => $value ){
    $url_foto = get_cfc_field( 'tim', 'fotka', false, $key );
    $header = get_cfc_field( 'tim','meno', false, $key );
    $position = get_cfc_field( 'tim','postavenie', false, $key );

    $output .= "<div class='b-team-member' style='background-image: url(" . $url_foto['url'] . ")'>";
    $output .= "<div class='b-team-text'>";
    $output .= "<h1>" . $header . "</h1>";
    $output .= "<divider></divider>";
    $output .= "<span>" . $position . "</span>";
    $output .= "</div>";
    $output .= "<div class='b-team-overlay'>";
    $output .= "</div>";
    $output .= "</div>";
    $output .= "</div>";
}
$output .= "</div>";
$output .= "</div>";

return $output;

}
add_shortcode( 'team', 'team_code' );