动态行wordpress的短代码

Dynamic Rows Shortcode for wordpress

本文关键字:代码 wordpress 动态      更新时间:2023-09-26

我正在尝试添加一个短代码来设置wordpress内容中的网格。我的主题是基于foundation5,所以我制作了一个名为"Shortcodes.php"的新文件,并将其加载到functions.php中

<?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
?>

因此,如果快捷代码处理第一个内容,我想打开一个新行。之后,我想设置列的宽度,如果短代码处理最后一个内容,则设置结束标记。之后,内容本身紧随其后,如果该行是最后一个内容,则后面跟着该行的结束div。

渲染的短代码看起来像

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

所以我期待类似的东西

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

或者像

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

所以我期待这样的东西

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

但它只是扼杀了我的模板。有什么想法或建议吗?

谢谢你们!

实际上它只是缺少点。。。。

<?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