简体   繁体   English

WordPress的动态行简码

[英]Dynamic Rows Shortcode for wordpress

i'm trying to add a shortcode to set a grid in wordpress content. 我正在尝试添加简码以在wordpress内容中设置网格。 My theme is based on foundation5 So I made a new file called "Shortcodes.php" and load it in the functions.php . 我的主题基于foundation5,所以我制作了一个名为“ Shortcodes.php”的新文件,并将其加载到functions.php中。 I inserted the following code to the shortcodes file 我将以下代码插入了简码文件

<?php 
function spalten_zeilen_function($atts, $content = null) {
    extract(shortcode_atts(array(
        'width' => '',
        'position' => '',
        'vertical' => '',
    ), $atts));

    if ( $position == 'first' ) {
        $return_string = '<div class="row '.$vertical.'">';
    }
    $return_string = '<div class="small-'.$width.' columns '.$position.';">';
    $return_string = do_shortcode($content);
    $return_string .= '</div>';
    if ( $position == 'end' ) {
        $return_string = '</div>';
    }

    wp_reset_query();
    return $return_string;
}
function register_shortcodes(){
    add_shortcode('grid_shortcode', 'spalten_zeilen_function');
}
add_action( 'init', 'register_shortcodes');
add_filter('widget_text', 'do_shortcode'); // Shortcodes auch in Widgets ausführen
add_filter( 'comment_text', 'do_shortcode' ); // Shortcodes auch in den Kommentaren ausführen
add_filter( 'the_excerpt', 'do_shortcode'); // Shortcodes auch in den Excerpts ausführen

?>

So what I want to do is to open a new row if the shortcode handles the first content. 因此,如果短代码处理了第一个内容,我想做的就是打开新行。 Afterwards I want to set the width of the column and, if the shortcode handles the last content an end tag. 之后,我想设置列的宽度,如果短代码处理了最后一个内容,则设置结束标记。 After that the content itself follows, followed by a closing div for the row if it's the last content. 之后,内容本身紧随其后,如果是最后一个内容,则紧跟该行的div结束。

The rendered shortcode looks like 呈现的简码看起来像

[grid_shortcode position="first" width="6" vertical="valign-top"]Fügen Sie hier den den gewünschten Inhalt Ihrer neuen Spalte ein.[grid_shortcode]

So I would expect something like 所以我期望像

<div class="row"><div class="small-6 columns first">
Fügen Sie hier den den gewünschten Inhalt Ihrer neuen Spalte ein.</div>

Or like 或喜欢

[grid_shortcode position="end" width="6" vertical="valign-top"]Fügen Sie hier den den gewünschten Inhalt Ihrer neuen Spalte ein.[grid_shortcode]

So I would expect somethin like 所以我希望像

<div class="small-6 columns end">Fügen Sie hier den den gewünschten Inhalt Ihrer neuen Spalte ein.</div></div>

But it just kills my template. 但这只会杀死我的模板。 Any idea or suggestions? 有什么想法或建议吗?

Thank you guys! 感谢大伙们!

Actually it was just missing dots.... 实际上,只是缺少点...。

<?php 
function spalten_zeilen_function($atts, $content = null) {
    extract(shortcode_atts(array(
        'position' => '',
        'width' => '',
        'vertical' => '',
    ), $atts));

    $return_string = '';
    if ( $position == 'first' ) :
        $return_string .= '<div class="small-12 columns"><div class="row '.$vertical.'">';
    endif;
    $return_string .= '<div class="small-'.$width.' columns '.$position.';">';
    $return_string .= do_shortcode($content);
    $return_string .= '</div>';
    if ( $position == 'end' ) :
        $return_string .= '</div></div>';
    endif;

    wp_reset_query();
    return $return_string;
}
function register_shortcodes(){
    add_shortcode('grid_shortcode', 'spalten_zeilen_function');
}
add_action( 'init', 'register_shortcodes');
add_filter('widget_text', 'do_shortcode'); // Shortcodes auch in Widgets ausführen
add_filter( 'comment_text', 'do_shortcode' ); // Shortcodes auch in den Kommentaren ausführen
add_filter( 'the_excerpt', 'do_shortcode'); // Shortcodes auch in den Excerpts ausführen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM