【问题标题】:Dynamic Rows Shortcode for wordpresswordpress 的动态行简码
【发布时间】:2015-01-05 07:02:08
【问题描述】:

我正在尝试添加简码以在 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>

但它只会杀死我的模板。有什么想法或建议吗?

谢谢你们!

【问题讨论】:

  • add_filter('...', 'do_shortcode') 应该做什么?
  • widget_text = 在小部件中使用时运行短代码 comment_text = 在评论中使用时运行短代码 the_excerpt = 在摘录中使用时运行短代码

标签: javascript php wordpress shortcode zurb-foundation-5


【解决方案1】:

其实只是少了点....

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

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多