将我的php代码转换为短码

时间:2014-05-07 作者:daxter

我有这种8块的<div> 所以我想把它转换成短代码:

<div class="homebx01 clearfix ani-1">
        <div class="homebxhead clearfix">
          <div class="bxicon homebxicon01"></div>
          <div class="bxheadtxt">Category name</strong></div>
        </div>
        <div class="cl"></div>
        <div class="homebxbody clearfix">
          <ul class="bxulsty">
            <li class="clearfix"><span></span> <strong>Category Description</strong></li>
          </ul>
        </div>
        <div class="cl"></div>
        <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
<div class="homebx01 clearfix ani-1">
        <div class="homebxhead clearfix">
          <div class="bxicon homebxicon01"></div>
          <div class="bxheadtxt">Category name</strong></div>
        </div>
        <div class="cl"></div>
        <div class="homebxbody clearfix">
          <ul class="bxulsty">
            <li class="clearfix"><span></span> <strong>Category Description</strong></li>
          </ul>
        </div>
        <div class="cl"></div>
        <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
<div class="homebx01 clearfix ani-1">
        <div class="homebxhead clearfix">
          <div class="bxicon homebxicon01"></div>
          <div class="bxheadtxt">Category name</strong></div>
        </div>
        <div class="cl"></div>
        <div class="homebxbody clearfix">
          <ul class="bxulsty">
            <li class="clearfix"><span></span> <strong>Category Description</strong></li>
          </ul>
        </div>
        <div class="cl"></div>
        <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
帮助我如何将我的这段php代码转换为要添加到页面中的短代码?

1 个回复
最合适的回答,由SO网友:totels 整理而成

创建短代码的基础非常简单。您只需创建一个函数,该函数返回要以字符串形式输出的代码,并注册该函数:

function wpse_143641_homebox_shortcode( $atts ) {
  return <<<HOMEBOX
<div class="homebx01 clearfix ani-1">
  <div class="homebxhead clearfix">
    <div class="bxicon homebxicon01"></div>
    <div class="bxheadtxt">Category name</strong></div>
  </div>
  <div class="cl"></div>
  <div class="homebxbody clearfix">
    <ul class="bxulsty">
      <li class="clearfix"><span></span> <strong>Category Description</strong></li>
    </ul>
  </div>
  <div class="cl"></div>
  <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
<div class="homebx01 clearfix ani-1">
  <div class="homebxhead clearfix">
    <div class="bxicon homebxicon01"></div>
    <div class="bxheadtxt">Category name</strong></div>
  </div>
  <div class="cl"></div>
  <div class="homebxbody clearfix">
    <ul class="bxulsty">
      <li class="clearfix"><span></span> <strong>Category Description</strong></li>
    </ul>
  </div>
  <div class="cl"></div>
  <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
<div class="homebx01 clearfix ani-1">
  <div class="homebxhead clearfix">
    <div class="bxicon homebxicon01"></div>
    <div class="bxheadtxt">Category name</strong></div>
  </div>
  <div class="cl"></div>
   <div class="homebxbody clearfix">
    <ul class="bxulsty">
      <li class="clearfix"><span></span> <strong>Category Description</strong></li>
    </ul>
  </div>
  <div class="cl"></div>
  <a class="morebt clearfix" href="#">Link to particular Categories post.</a> 
</div>
HOMEBOX;
}
add_shortcode( \'homebox\', \'wpse_143641_homebox_shortcode\' );
有关如何接受aguments的更多示例和详细信息,请参阅Shortcode_API 法典文件。

结束

相关推荐

Manipulated shortcode output

我开发的一个注册短代码的插件有问题。短代码返回一个包含有效HTML的字符串,但一些主题似乎操纵了短代码返回的HTML,我真的不明白原因是什么。例如,这是我的短代码的正确输出:<div class=\"tile\"> <a> <img src=\"0.jpg\" /> <div class=\"caption\"> <p>Kate</p> </div&