如何创建包含html和php内容的快捷代码?

时间:2017-05-22 作者:Nienormalny_

我想为其中一个内容表单主题创建一个快捷码。

我的模板内容:

<section id="standing"  style="background-image: url(\'<?php echo (get_option(\'pixieclash-standings-background\') && get_option(\'pixieclash-standings-background\') != null) ? esc_url(get_option(\'pixieclash-standings-background\')) : get_stylesheet_directory_uri() . \'/images/standings-bg.jpg\' ?>\');">

<div class="container">
    <?php
        $headTxt = get_option(\'pixieclash-standings-section-heading-top\');
        $bottomText = get_option(\'pixieclash-standings-section-heading-bottom\');
    ?>

    <?php if(!empty($headTxt) || !empty($bottomText)): ?>
    <article class="head">
        <?php if(!empty($headTxt)): ?>
            <h4><?php echo esc_attr($headTxt); ?></h4>
        <?php endif; ?>

        <?php if(!empty($bottomText)): ?>
            <h3><?php echo esc_attr($bottomText); ?></h3>
        <?php endif; ?>
    </article>
    <?php endif; ?>

    <?php
        $groups = pixieclash_standings_groups();

        if(!empty($groups)):
            $i = 1;
            $onlyOne = count($groups) > 1 ? \'\' : \'onlyOne\';
            foreach($groups as $group):
                $competitors = pixieclash_standings($group[\'id\']);
    ?>
    <article class="table <?php echo ($i % 2 != 0) ? \'first\' : \'second\' ?> <?php echo $onlyOne ?>">
        <h5 class="group-name"><?php esc_html_e(\'Group\', \'pixieclash\') ?> <?php echo esc_attr($group[\'name\']) ?></h5>

        <table class="table-body">
            <thead>
            <tr>
                <th><?php esc_html_e(\'Competitor\', \'pixieclash\') ?></th>
                <th rel="match<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'M\', \'pixieclash\') ?></th>
                <th rel="win<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'W\', \'pixieclash\') ?></th>
                <th rel="till<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'T\', \'pixieclash\') ?></th>
                <th rel="loss<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'L\', \'pixieclash\') ?></th>
                <th rel="point<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'P\', \'pixieclash\') ?></th>
            </tr>
            </thead>
            <tbody>
            <?php if(!empty($competitors)): foreach($competitors as $competitor): ?>
            <tr>
                <td>
                    <figure>
                        <img src="<?php echo esc_url($competitor[\'logo\']) ?>" alt="<?php echo esc_attr($competitor[\'name\']) ?>">
                    </figure>
                    <?php echo esc_attr($competitor[\'name\']) ?>
                </td>
                <td rel="match<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'played\']) ?></td>
                <td rel="win<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'wins\']) ?></td>
                <td rel="till<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'till\']) ?></td>
                <td rel="loss<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'losses\']) ?></td>
                <td rel="point<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'points\']) ?></td>
            </tr>
            <?php endforeach; else: ?>
            <tr>
                <td colspan="6"><?php esc_html_e(\'No competitors\', \'pixieclash\') ?></td>
            </tr>
            <?php endif; ?>
            </tbody>
        </table>

    </article>

    <?php $i ++; endforeach; endif; ?>
</div>
</section>
我尝试使用add_shorcode(\'table\', \'name of this function\'); 但这对我不起作用。

我该怎么做?

2 个回复
SO网友:Johansson

要从函数中创建短代码,需要使用以下代码:

function my_shortcode_function(){ 
    if (get_option(\'pixieclash-standings-background\') && get_option(\'pixieclash-standings-background\') != null) {
        $background = esc_url(get_option(\'pixieclash-standings-background\')) 
    } else {
        $background = get_stylesheet_directory_uri() . \'/images/standings-bg.jpg\';
    }
    $headTxt = get_option(\'pixieclash-standings-section-heading-top\');
    $bottomText = get_option(\'pixieclash-standings-section-heading-bottom\'); 
    if(!empty($headTxt)){ 
        $text_header = \'<h4>\'. esc_attr($headTxt);.\'</h4>\';
    } else {
        $text_header = \'\';
    }
    if(!empty($bottomText)) { 
        $text_footer = \'<h3>\'.esc_attr($bottomText); .\'</h3>\';
    } else {
        $text_footer =\'\';
    }
    if(!empty($headTxt) || !empty($bottomText)){ 
        $container = \'<article class="head">\'. $text_header . $text_footer.\'</article>\';
    } 
    $groups = pixieclash_standings_groups();
    if(!empty($groups)) {
        $i = 1;
        $onlyOne = (count($groups) > 1 ) ? \'\' : \'onlyOne\';
        $competitors =\'\';
        foreach($groups as $group){
            $competitors .= pixieclash_standings($group[\'id\']);
        }
    }
    $data = \'
        <section id="standing" style="background-image: url("\'.$background.\'");">
            <div class="container">\'.$container.$competitors\'</div>
        </section>\';
    return $data;
}
add_action(\'my-table-shortcode\',\'pixieclash_standings_two\');
确保为短代码选择唯一的名称。

现在使用以下命令输出您的短代码:

[my-table-shortcode]

或者,使用do_shortcode() 在PHP文件中:

echo do_shortcode(\'[my-table-shortcode]\');

PS : 请注意,因为我没有访问你的函数的权限,所以我无法准确地验证代码。如果代码没有按预期工作,您可以检查错误日志中的任何问题。

UPDATE

有一个真正简单的解决方案,我认为在您的情况下效果更好,它正在使用ob_start(). 这将记录内容的每个输出,并将其存储在一个值中,以便稍后返回。看看这个:

function my_content_shortcode(){ 
    ob_start();?>
    <section id="standing"  style="background-image: url(\'<?php echo (get_option(\'pixieclash-standings-background\') && get_option(\'pixieclash-standings-background\') != null) ? esc_url(get_option(\'pixieclash-standings-background\')) : get_stylesheet_directory_uri() . \'/images/standings-bg.jpg\' ?>\');">

    <div class="container">
        <?php
            $headTxt = get_option(\'pixieclash-standings-section-heading-top\');
            $bottomText = get_option(\'pixieclash-standings-section-heading-bottom\');
        ?>

        <?php if(!empty($headTxt) || !empty($bottomText)): ?>
        <article class="head">
            <?php if(!empty($headTxt)): ?>
                <h4><?php echo esc_attr($headTxt); ?></h4>
            <?php endif; ?>

            <?php if(!empty($bottomText)): ?>
                <h3><?php echo esc_attr($bottomText); ?></h3>
            <?php endif; ?>
        </article>
        <?php endif; ?>

        <?php
            $groups = pixieclash_standings_groups();

            if(!empty($groups)):
                $i = 1;
                $onlyOne = count($groups) > 1 ? \'\' : \'onlyOne\';
                foreach($groups as $group):
                    $competitors = pixieclash_standings($group[\'id\']);
        ?>
        <article class="table <?php echo ($i % 2 != 0) ? \'first\' : \'second\' ?> <?php echo $onlyOne ?>">
            <h5 class="group-name"><?php esc_html_e(\'Group\', \'pixieclash\') ?> <?php echo esc_attr($group[\'name\']) ?></h5>

            <table class="table-body">
                <thead>
                <tr>
                    <th><?php esc_html_e(\'Competitor\', \'pixieclash\') ?></th>
                    <th rel="match<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'M\', \'pixieclash\') ?></th>
                    <th rel="win<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'W\', \'pixieclash\') ?></th>
                    <th rel="till<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'T\', \'pixieclash\') ?></th>
                    <th rel="loss<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'L\', \'pixieclash\') ?></th>
                    <th rel="point<?php echo esc_attr($group[\'id\']) ?>"><?php esc_html_e(\'P\', \'pixieclash\') ?></th>
                </tr>
                </thead>
                <tbody>
                <?php if(!empty($competitors)): foreach($competitors as $competitor): ?>
                <tr>
                    <td>
                        <figure>
                            <img src="<?php echo esc_url($competitor[\'logo\']) ?>" alt="<?php echo esc_attr($competitor[\'name\']) ?>">
                        </figure>
                        <?php echo esc_attr($competitor[\'name\']) ?>
                    </td>
                    <td rel="match<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'played\']) ?></td>
                    <td rel="win<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'wins\']) ?></td>
                    <td rel="till<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'till\']) ?></td>
                    <td rel="loss<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'losses\']) ?></td>
                    <td rel="point<?php echo esc_attr($group[\'id\']) ?>"><?php echo esc_attr($competitor[\'points\']) ?></td>
                </tr>
                <?php endforeach; else: ?>
                <tr>
                    <td colspan="6"><?php esc_html_e(\'No competitors\', \'pixieclash\') ?></td>
                </tr>
                <?php endif; ?>
                </tbody>
            </table>

        </article>

        <?php $i ++; endforeach; endif; ?>
    </div>
    </section><?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_action(\'my-table-shortcode\',\'my_content_shortcode\');
现在,使用[my-table-shortcode] 在文本窗口小部件或内容中,将完全作为您的部分,与使用echo do_shortcode(\'[my-table-shortcode]\'); 在php文件中。

SO网友:Niket Joshi

//用于生成提要栏daynamic函数sidebar\\u detail\\u payedpost的快捷码($Shortcode\\u attr,$Shortcode\\u content){

      // echo "sidebar";
         $current_cat_id = get_queried_object_id();
         $count_post=get_term($current_cat_id);
         $city = isset($_GET[\'locationID\']) ? base64_decode($_GET[\'locationID\']) : $_SESSION[\'prefer_city\'];
         $args = array(
              \'post_type\' => \'classified\',
              \'tax_query\' => array( 
                                 array(
                                    \'taxonomy\' => \'classified_location\',
                                    \'field\'    => \'id\',
                                    \'terms\'    => array($city),
                                    \'operator\' => \'IN\',
                                  )
                 ),
              );
            $result = new WP_Query($args);
            // echo $result->found_posts;
           //  print_r($result);
          //   echo "</pre>";
         if ( $result-> have_posts() ){

         while ( $result->have_posts() ){
             $result->the_post();
            ?>
          <!-- <div class="container post_box"> -->
           <div class="container">

                <?php 
                    $data=get_the_post_thumbnail();
                 ?>
                 <div class="SidebarPost">
                <a href="<?php echo get_permalink();  ?>"  data-img=\'<?php echo ($data); ?>\' class="postimage_sidebar" ><?php echo ($data); ?>
                </a> 
                 <h5><a href="<?php echo get_permalink();  ?>"><?php the_title(); ?></a></h5>
                </div>   
            </div>


        <?php    
         }
    } 

}
    add_shortcode(\'DisplayPayablePost\', \'sidebar_detail_payedpost\');
这段代码在侧栏中显示付费帖子,对我来说效果很好。你可以这样添加HTML和PHP。

这是经过充分测试和正确的代码,我只希望这对你有用。

结束

相关推荐

从WordPress外部获取ShortCode属性

有没有一种干净的方法可以在Wordpress的默认行为之外获取短代码的属性值?我正在Wordpress安装之外编写一个脚本include(\'../wp-load.php\'); 在一开始。就我而言,我希望\"caption\" 属性[caption] shortcode 我发现最好的方法如下,但我对此很不满意。原帖子内容:[caption id=\"attachment_1\" align=\"alignleft\" width=\"150\" caption=\"I\'m a wicked cool