【问题标题】:WordPress Shortcode - Change Layout Based On AttributeWordPress 简码 - 根据属性更改布局
【发布时间】:2013-05-09 00:04:23
【问题描述】:

我有以下自定义简码,它以定义的“类型”分类显示员工的自定义帖子类型。我正在尝试添加第二个属性,该属性将允许用户定义输出的布局样式,例如列或行。这是我设想的短代码的样子:

[staff type="international" style="rows"]

我认为我拥有获取属性的所有编码,包括设置默认值,但我似乎无法弄清楚如何在其中获取 if 语句来更改输出。看起来它应该在第一条“$output .=" 行之前。

function get_staff($atts) {
    extract( shortcode_atts( array( 
        'type' => 'international', 
        'style' => 'rows'
        ), $atts ) );

    add_filter( 'posts_orderby' , 'posts_orderby_lastname' );

    $loop = new WP_Query(
        array (
            'post_type' => 'staff',
            'orderby' => 'title',
            'staff-type' => $type,
            'style' => $style
        )
    );

    remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );

    if ($loop->have_posts()) {
        $output = '<div class="staff">';

        while($loop->have_posts()){
            $loop->the_post();
            $meta = get_post_meta(get_the_id());
            // Attributes Array for Featured Image Below
            $attr = array(
                                'title' => get_the_title(),
                                'alt' => get_the_title(),
                                'class' => 'img_frame'
                            );
            $output .= '
                <div class="row-fluid" style="border-bottom: 1px solid #EEE; margin-bottom: 16px; padding-bottom: 16px;">
                <div class="span3">' . get_the_post_thumbnail($post->ID, 'small', $attr) . '</div>
                <div class="span9"><h3>' . get_the_title()  . '</h3>
                ' . get_the_content() . '
            </div></div>
        ';
    }
    $output .= "</div>";
} else {
    $output = 'No Staff Meet This Criteria Yet.';
}

return $output;
};

// Create Last Name Sort For Staff Custom Post Type
function posts_orderby_lastname ($orderby_statement) 
{
  $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
    return $orderby_statement;
}


add_shortcode('staff', 'get_staff'); 

【问题讨论】:

    标签: wordpress attributes custom-post-type shortcode


    【解决方案1】:

    有几种方法,具体取决于您的编码风格。您可以检查style 参数before 开始循环遍历帖子。另一种方法是在 开始循环之后进行。当然,有一些代码重复,但它更易于阅读,并且您将拥有对要生成的确切标记的所有控制。

    $output = '<div class="staff">';
    
    if ($loop->have_posts()) {
    
        if($style == 'rows'){ // rows
            $output .= '<div class="layout-rows">';
            while( $loop->have_posts() ){
                // ...
            }
            $output .= "</div>";
        } elseif($style == 'columns') { // columns
            $output .= '<div class="layout-columns">';
            while( $loop->have_posts() ){
                // ...
            }
            $output .= "</div>";
        } else { // everything else
            $output .= '<div class="layout-default">';
            while( $loop->have_posts() ){
                // ...
            }
            $output .= "</div>";
        }
    
    } else {
        $output = 'No Staff Meet This Criteria Yet.';
    }
    
    $output .= "</div>";
    

    【讨论】:

    • 谢谢,现在我明白了,这很有意义。不知道我做错了什么,但这正是我需要的。
    • 干杯。我认为问题是在if 内打开&lt;div class="staff"&gt; 并在外面关闭它。
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2013-06-22
    • 2017-06-29
    相关资源
    最近更新 更多