添加Genesis作者框内容包装器

时间:2017-04-29 10:55:21

标签: php genesis

我需要在作者框内的所有内容周围添加一个包装器div。

将genesis_author_box更改为:

需要哪些过滤器代码
<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
  <div class="author-box-wrap">
    <img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
    <h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
    <div class="author-box-content" itemprop="description">Description Text</div>
  </div>
</section>

这是默认的Genesis标记:

<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
  <img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
  <h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
  <div class="author-box-content" itemprop="description">Description Text</div>
</section>

以下是组装默认Genesis标记的代码:

/**
* Echo the the author box and its contents.
*
* The title is filterable via `genesis_author_box_title`, and the gravatar size is filterable via
* `genesis_author_box_gravatar_size`.
*
* The final output is filterable via `genesis_author_box`, which passes many variables through.
*
* @since 1.3.0
*
* @global WP_User $authordata Author (user) object.
*
* @param string $context Optional. Allows different author box markup for different contexts, specifically 'single'.
*                        Default is empty string.
* @param bool   $echo    Optional. If true, the author box will echo. If false, it will be returned.
* @return string HTML for author box if `$echo` param is falsy.
*/
function genesis_author_box( $context = '', $echo = true ) {

global $authordata;

$authordata    = is_object( $authordata ) ? $authordata : get_userdata( get_query_var( 'author' ) );
$gravatar_size = apply_filters( 'genesis_author_box_gravatar_size', 70, $context );
$gravatar      = get_avatar( get_the_author_meta( 'email' ), $gravatar_size );
$description   = wpautop( get_the_author_meta( 'description' ) );

// The author box markup, contextual.
if ( genesis_html5() ) {

    $title = __( 'About', 'genesis' ) . ' <span itemprop="name">' . get_the_author() . '</span>';

    /**
     * Author box title filter.
     *
     * Allows you to filter the title of the author box. $context passed as second parameter to allow for contextual filtering.
     *
     * @since unknown
     *
     * @param string $title   Assembled Title.
     * @param string $context Context.
     */
    $title = apply_filters( 'genesis_author_box_title', $title, $context );

    $heading_element = 'h1';

    if ( 'single' === $context && ! genesis_get_seo_option( 'semantic_headings' ) ) {
        $heading_element = 'h4';
    } elseif ( genesis_a11y( 'headings' ) || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
        $heading_element = 'h4';
    }

    $pattern  = sprintf( '<section %s>', genesis_attr( 'author-box' ) );
    $pattern .= '%s<' . $heading_element . ' class="author-box-title">%s</' . $heading_element . '>';
    $pattern .= '<div class="author-box-content" itemprop="description">%s</div>';
    $pattern .= '</section>';

} else {

    $title = apply_filters( 'genesis_author_box_title', sprintf( '<strong>%s %s</strong>', __( 'About', 'genesis' ), get_the_author() ), $context );

    $pattern = '<div class="author-box">%s<h1>%s</h1><div>%s</div></div>';

    if ( 'single' === $context || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
        $pattern = '<div class="author-box"><div>%s %s<br />%s</div></div>';
    }

}

$output = sprintf( $pattern, $gravatar, $title, $description );

/**
 * Author box output filter.
 *
 * Allows you to filter the full output of the author box.
 *
 * @since unknown
 *
 * @param string $output  Assembled output.
 * @param string $context Context.
 * @param string $pattern (s)printf pattern.
 * @param string $context Gravatar.
 * @param string $context Title.
 * @param string $context Description.
 */
$output = apply_filters( 'genesis_author_box', $output, $context, $pattern, $gravatar, $title, $description );

if ( $echo ) {
    echo $output;

    return null;
} else {
    return $output;
}

我的PHP非常粗糙...感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

尝试一下:

add_filter( 'genesis_author_box', 'my_filter_author_box', 10, 3 );
function my_filter_author_box( $output, $context ) {
    $output = preg_replace( '/<section (.+?)>(.+)<\/section>/ms', '<section $1><div class="author-box-wrap">$2</div></section>', $output );

    return $output;
}

可能值得重新考虑为什么你需要添加内包装div,看看你是否可以找到一种更好的方式来代替它。