【问题标题】:Hide specific part of Shortcode if attributes are not set如果未设置属性,则隐藏短代码的特定部分
【发布时间】:2019-10-24 16:18:51
【问题描述】:

我创建了以下短代码来显示块引用:

// Add Shortcode
function quote_shortcode( $atts , $content = null ) {

// Attributes
$atts = shortcode_atts(
    array(
        'author' => 'author',
        'author_job' => 'author_job',
    ),
    $atts
);
return
'<div data-module="expert_quote"><blockquote class="full no-picture"><p>“' . $content . '”</p><footer class="quote-footer"><cite><span class="name">' . esc_attr($atts['author']) . '</span> <span class="title">' . esc_attr($atts['author_job']) . '</span></cite></footer></blockquote></div>';
}

add_shortcode( 'quote', 'quote_shortcode' );

我不想回来

<span class="name">' . esc_attr($atts['author']) . '</span> 

如果author 未在短代码中设置。 author_job 也是如此。

我怎样才能做到这一点?

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    您需要有条件地创建返回字符串。您可以使用以下代码:

    function quote_shortcode( $atts , $content = null ) {
    
    // Attributes
    $atts = shortcode_atts(
        array(
            'author' => 'author',
            'author_job' => 'author_job',
        ),
        $atts
    );
    
    $return_string = '<div data-module="expert_quote">';
    $return_string .= '<blockquote class="full no-picture">';
    $return_string .= '<p>“' . $content . '”</p>';
    $return_string .= '<footer class="quote-footer">';
    $return_string .= '<cite>';
        if (isset($atts['author'])) {
            $return_string .= '<span class="name">' . esc_attr($atts['author']) . '</span>';
        }
        if (isset($atts['author_job'])) {
            $return_string .= '<span class="title">' . esc_attr($atts['author_job']) . '</span>';
        }
    $return_string .= '</cite>';
    $return_string .= '</footer">';
    $return_string .= '</blockquote">';
    $return_string .= '</div">';
    
    return $return_string;
    }
    
    add_shortcode( 'quote', 'quote_shortcode' );
    

    【讨论】:

    • 其实我刚试过。它不起作用,因为代码只输出最后一个 $return_string
    【解决方案2】:

    我已经设法让它工作,但不确定我的代码是否得到了很好的优化:

    function quote_shortcode( $atts , $content = null ) {
    
    // Attributes
    $atts = shortcode_atts(
        array(
            'author' => '',
            'author_job' => '',
        ),
        $atts
    );
    
    $return_string = '<div data-module="expert_quote">';
    $return_string .= '<blockquote class="full no-picture">';
    $return_string .= '<p>“' . $content . '”</p>';
    if (!empty($atts['author']) || !empty($atts['author_job'])) {
      $return_string .= '<footer class="quote-footer">';
      $return_string .= '<cite>';
      }
        if (!empty($atts['author'])) {
            $return_string .= '<span class="name">' . esc_attr($atts['author']) . '</span>';
        }
        if (!empty($atts['author_job'])) {
            $return_string .= '<span class="title">' . esc_attr($atts['author_job']) . '</span>';
        }
    if (!empty($atts['author']) && !empty($atts['author_job'])) {
        $return_string .= '</cite>';
        $return_string .= '</footer>';
    }
    $return_string .= '</blockquote>';
    $return_string .= '</div>';
    
    return $return_string;
    }
    
    add_shortcode( 'quote', 'quote_shortcode' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2013-03-04
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多