doulan9188 2016-07-04 10:51
浏览 39

在短代码wordpress中插入多个短代码

I am a new to php and would like to know how insert multiple shortcodes with shortcodes

the desired result would look something like this

[logo_section_2 section-title="1" section-title-text="WE SHARE THE SAME CULTURE" logos-second-row="true"]
  [logo_images logo-id="1" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-swiss.png"]
  [logo_images logo-id="2" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-hudson.png"]
  [logo_images logo-id="3" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-twitter.png"]
  [logo_images logo-id="4" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-index.png"]
  [logo_images logo-id="5" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-reed.png"]
  [logo_images logo-id="6" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-viagogo.png"]

  [logo_images_2 logo-id="4" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-index.png"]
  [logo_images_2 logo-id="5" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-reed.png"]
  [logo_images_2 logo-id="6" logo-image-source="http://localhost:8888/signal-theme-export/wordpress/wp-content/uploads/2016/07/logo-viagogo.png"]

[/logo_section_2]

This is so I can add multiple rows of images

my php is as below

<?php

add_shortcode('logo_section_2', 'logo_section_2'); function logo_section_2( $atts, $content = "" ) { shortcode_atts( array( 'section-title' => false, 'section-title-text' => '', 'logos-second-row' => false, // ..... ),$atts);

    $output = '';
    $output .= '<div class="logo-section clearfix">';   
      $output .= '<div class="logo-section--inner clearfix">'; 
          if ($atts['section-title'] == true) {
          $output .= '<h2 class="heading--small heading--bold heading--uppercase heading--sans-serif heading--center heading">'. $atts['section-title-text'] .'</h2>';
          } else {
                  $output .= '';
          }

          $output .=  '<div class="logo-section--row clearfix">' . do_shortcode($content) . '</div>';

          if ($atts['logos-second-row'] == 'true') {
            $output .=  '<div class="logo-section--row clearfix">' . do_shortcode($content) . '</div>';
          } else {
                  $output .= '';
          }
      $output .= '</div>'; 
    $output .= '</div>';
  return $output;

}

add_shortcode('logo_images', 'logo_images');
function logo_images($atts){
  shortcode_atts(
    array(
      'logo-id' => '',
      'logo-image-source' => '',
      'logo-alt' => '',
    ),$atts);
    $output = '';
    if($atts['logo-id'] || $atts['logo-image-source']){
               $output .= '<img class="logo-'.$atts['logo-id'].'" src="' . $atts['logo-image-source'] . '" alt="" />';
    } else {
            $output .= '';
    }
    return $output;
 }
add_shortcode('logo_images_2', 'logo_images_2');
function logo_images_2($atts){
  shortcode_atts(
    array(
      'logo-id' => '',
      'logo-image-source' => '',
      'logo-alt' => '',
    ),$atts);
    $output = '';
    if($atts['logo-id'] || $atts['logo-image-source']){
               $output .= '<img class="logo-'.$atts['logo-id'].'" src="' . $atts['logo-image-source'] . '" alt="" />';
    } else {
            $output .= '';
    }
    return $output;
 }

any help would be awesome!

  • 写回答

1条回答 默认 最新

  • doutan2456 2016-07-04 22:35
    关注

    I think what you're asking is about nested shortcodes... you want to trigger a UI element that may have one or more (variable number) internal items.

    This concept is difficult to explain because it requires several code concepts put together. But I will try:

    1. Outer Shortcode:

      a. Set a global to test if already in that shortcode

      b. call do_shortcode($content) to execute inner shortcodes

      c. (Inner shortcode will set up an global array)

      d. Generate full UI using a loop on the global array

      e. return the output

    2. Inner Shortcode:

      a. set up a global array to be used by the outer shortcode

      b. generate the content and place the output in an array item

      c. return blank/no output

    It's not that straight forward tho... you'll need to consider multiple outer shortcode calls, incorrectly nested shortcodes, and if you want or don't want non-shortcode in the inner layer to be processed or not.

    Have a look at this code... maybe you can extract some ideas from it.

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程