我是php的新手,想知道如何使用短代码插入多个短代码
所需的结果看起来像这样
[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]
这样我可以添加多行图像
我的php如下
<?php
add_shortcode('logo_section_2','logo_section_2'); function logo_section_2($ atts,$ content =“”){ shortcode_atts( 阵列( 'section-title'=&gt;假, 'section-title-text'=&gt; ', 'logos-second-row'=&gt;假, // ..... ),$的ATT);
$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;
}
任何帮助都会很棒!
答案 0 :(得分:0)
我认为你要问的是关于嵌套的短代码...你想要触发一个可能有一个或多个(可变数量)内部项的UI元素。
这个概念很难解释,因为它需要将几个代码概念放在一起。但我会尝试:
外部短代码:
一个。设置全局以测试是否已存在于该短代码中
湾调用do_shortcode($content)
执行内部短代码
℃。 (内部短代码将设置一个全局数组)
d。使用全局数组上的循环生成完整的UI
即返回输出
内部短代码:
一个。设置一个由外部短代码
使用的全局数组湾生成内容并将输出放在数组项
中℃。返回空白/无输出
这不是那么直接......你需要考虑多个外部短代码调用,错误嵌套的短代码,如果你想要或者不想在内部使用非短代码要处理的图层。
看看这个code ...也许你可以从中提取一些想法。