Скрыть определенную часть Shortcode, если атрибуты не установлены - PullRequest
0 голосов
/ 10 июня 2019

Я создал следующий шорткод для отображения цитат:

// 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.

Как мне этого добиться?

Ответы [ 2 ]

1 голос
/ 10 июня 2019

Вам необходимо создать строку возврата условно.Вы можете использовать следующий код:

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' );
0 голосов
/ 12 июня 2019

Мне удалось заставить его работать, но я не уверен, что мой код хорошо оптимизирован:

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' );
...