我正在创建一个插件,该插件当前从数据库中返回商店库存。
现在,我只是输出原始文本。
我想做的是输出数据并让其他短代码呈现数据。
例如:
[store_inventory]
[/store_inventory]
上面的短代码将返回以下内容
array([0]=['item_name'='Juice', 'item_number' = '3dsj'], [1]=['item_name'='bread', 'item_number' = 'br3d']);
我想做的是让store_inventory短代码遍历整个数组,而不是返回原始数组。并将每个循环返回的返回值传递给另一组短代码,这样我就可以将数据写入其自己的html中。
我的想法看起来像这样
[store_inventory] //This shortcode loops through the inventory array returned from the database
<div>
<p>[item_name]</p>//This shortcode returns current item_name being looped
<p>[item_number]</p>//This shortcode returns current item_number being looped
</div>
[/store_inventory]
我只是不确定如何处理遍历数组并将当前数据记录从数组传递到其他两个短代码。
任何帮助将不胜感激。
我知道,只需吐出已从插件格式化的HTML就会很容易,但这将意味着无需通过wordpress进行前端编辑或通过wordpress进行版本控制。
答案 0 :(得分:0)
您必须遍历store_inventory
中的每个项目并在do_shortcode
中传递数据。
我不确定您的store_inventory
短代码是什么样子,但请参见以下示例:
function story_inventory_loop( $atts ) {
extract( shortcode_atts( array(
//attributes
), $atts ) );
$output = '<div>';
$args = array(
'post_type' => 'post', //your post type
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$output .= '<p>'.
echo do_shortcode( '[item_name]' . get_the_title() . '[/item_name]' ).
'</p>'.
'<p>'.
echo do_shortcode( '[item_number]' . get_the_excerpt(). '[/item_number]' ).
'</p><!-- ends here -->';
endwhile;
wp_reset_query();
$output .= '</div>';
return $output;
}
add_shortcode('store_inventory', 'story_inventory_loop');
item_name
短代码:
function item_name_shortcode( $atts, $content = null ) {
return $content ;
}
add_shortcode( 'item_name', 'item_name_shortcode' );
item_number
短代码:
function item_number_shortcode( $atts, $content = null ) {
return $content ;
}
add_shortcode( 'item_number', 'item_number_shortcode' );
希望这会有所帮助。