【问题标题】:How to add title attribute when a page has a particular template当页面具有特定模板时如何添加标题属性
【发布时间】:2013-01-13 10:08:51
【问题描述】:

在我的管理页面中,我可以将默认模板页面更改为另一个自定义模板。 我需要的是:

在我的导航标题菜单中,我有许多链接,当a 标记指向将使用我的自定义模板而不是默认模板呈现的页面时,我必须设置a 标记的标题属性。示例:

<li><a hreh=".." title="myCustom">link1</a></li> //this title will be redirected with my custom template
<li><a hreh="..">link2</a></li> //this title will be redirected with default template
<li><a hreh="..">link3</a></li> //this title will be redirected with default template
<li><a hreh=".." title="myCustom">link4</a></li> //this title will be redirected with my custom template

如果我打开 header.php 链接创建者:

<?php $params = array( 
      'theme_location'  =>'primary',
      'limit'  => 5,
      'format' => 'custom',
      'link_before' => '<span>',
      'link_after'  => '</span>' );
       wp_nav_menu($params); 
?>

如何检查链接是由默认模板呈现还是由我的模板呈现?

【问题讨论】:

  • 我试着更清楚地编辑帖子.. 等一下 :)

标签: wordpress templates attributes


【解决方案1】:

我不确定您要做什么,但关于 title 的事情,您可以轻松地在后端添加它...外观 > 菜单。下面的示例图片..


我不确定这是否会有所帮助,但您是否尝试过阅读Creating Your Own Page Templates

因此,当您创建页面时,您可以选择用于该特定页面的模板。像下面这张图,

【讨论】:

  • 我知道我可以手动执行此操作..但我试图自动执行...如果页面由我的自定义模板呈现 ->set title=...
【解决方案2】:

如果您的主题使用功能 body_class,例如 &lt;body &lt;?php body_class(); ?&gt;&gt;,则您的 Html 已经在打印您需要的内容以仅在该页面中定位元素:

此页面正在使用模板文件:/twentytwelve/page-templates/front-page.php

你看到那里有一个 StackOverflow 类;),那是:

add_filter('body_class', 'body_class_so_14302462');

function body_class_so_14302462( $classes ) 
{
    global $post;
    if( 'page' != $post->post_type )
        return $classes;

    $classes[] = 'STACKOVERFLOW';
    return $classes;
}

您可以使用$classes = 'custom-and-only-class'; 完全覆盖它,而不是向数组中添加一个类,还可以进行各种检查以微调输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多