首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Genesis -如何将额外的类传递给genesis_markup?

Genesis -如何将额外的类传递给genesis_markup?
EN

Stack Overflow用户
提问于 2015-04-28 00:02:27
回答 1查看 1.4K关注 0票数 1

我有一个创建名为"content-wrapper“的div的函数。在特殊情况下,我需要在这个div中添加额外的类,我尝试使用这个函数:

代码语言:javascript
复制
function content_wrapper($function, $title = '', $first = false ) {
    if ('open' != $function && 'close' != $function)
        return;

if ('open' == $function) {
    if ($first == true) {
        genesis_markup( array(
            'html5'   => '<div %s>',
            'xhtml'   => '<div class="content-wrapper content-wrapper-first">',
            'context' => 'content-wrapper content-wrapper-first',
        ) );

但是输出结果是

代码语言:javascript
复制
<div class="content-wrappercontent-wrapper-first">

有没有人知道为什么要删除空格以及如何添加空格?我甚至将函数扩展为

代码语言:javascript
复制
function content_wrapper($function, $title = '', $args = '' ) {

$args将是一个数组,我可以在其中传递额外的类,但这不能正常工作。甚至genesis_attr-content-wrapper也不能正常工作,因为它向页面上的每个内容包装器添加了额外的类。

有没有人有主意?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-05-06 21:58:50

看起来在genesis主题的markup.php文件中的函数genesis_parse_attr()中的空格在运行sanitize_html_class($context)时被删除了。您对使用genesis_attr- context -wrapper过滤器的想法是正确的,但此上下文在其他地方使用,因此它将被多次调用。对于仅在需要时才添加类的情况,将上下文更改为“content-wrapper-first”,这将创建一个名为genesis_attr- context -wrapper-first的过滤器。连接到这个过滤器并添加上下文包装器类(以及您想要的任何其他类)。

所以像这样调用genesis_markup:

代码语言:javascript
复制
genesis_markup( array(
  'html5'   => '<div %s>',
  'xhtml'   => '<div class="content-wrapper content-wrapper-first">',
  'context' => 'content-wrapper-first',
 ) );

然后挂钩到过滤器,只有当genesis_markup具有上下文“content-wrapper-first”时才会调用该过滤器

代码语言:javascript
复制
add_filter('genesis_attr_content-wrapper-first','myFilterFunction');

function myFilterFunction($attributes) {
  $attributes['class'] = $attributes['class'] . ' ' . 'content-wrapper';
  return $attributes;
}

因为上下文是内容包装器优先的,所以它已经在$attributes['class']变量中了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29900654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档