【问题标题】:Node template for blog teaser node博客预告节点的节点模板
【发布时间】:2010-08-23 18:33:05
【问题描述】:

我正在尝试创建一个预告片节点模板来显示所有博客预告片。 对于页面 tpl 我有 page-blogs.tpl.php 对于博客节点 tpl,我有 node-blog.tpl.php (这是循环显示所有博客预告片) 现在如何创建一个节点模板来围绕节点预告片? 我的所有博客预告页面的 URL 是:/blogs/eric 我的带有示例博客条目的页面的 URL 是:/blogs/eric/test-blog-1 我需要一个可用于所有博客页面的节点模板。 我尝试将 node-blogs-teaser.tpl.php 用于单个预告片节点,将 node-blog.tpl.php 用于外部博客节点模板,但这不起作用。

这是我的 node-blog.tpl.php 文件中的内容:

<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<div class="item">
<ul>
<?php print $picture ?>

<?php if ($page == 0): ?>
<?php endif; ?>

  <div class="content clear-block">

    <li class="title"><h4><?php print $title ?></h4></li>
    <li class="photo"><a href="#"><img src="/<?php print $node->field_blog_image[0]['filepath']; ?>" /></a></li>
    <li class="desc"><?php print $node->content['body']['#value']; ?></li>
    <li class="link">
    <?php if ($teaser): ?>
    <a href="<?php print $node_url ?>" class="block-link">Read more</a> | <a href="<?php print $node_url ?>" class="block-link">Audio/Video</a> | 
    <?php endif; ?>
    <?php print $submitted; ?>
    </li>
    <div class="clear"></div>     

  </div>

  <div class="clear-block">
    <div class="meta">
    <?php if ($taxonomy): ?>
      <div class="terms"><?php print $terms ?></div>
    <?php endif;?>
    </div>

  </div>
</ul>
</div>
</div>

谢谢

更新:我在 template.php 中添加了页面预处理器功能:

/**
 * Override or insert PHPTemplate variables into the templates.
 * These are the main outer templates such as page.tpl.php 
 */
function phptemplate_preprocess_page(&$vars) {

    // add template hints using path alias
    $alias = drupal_get_path_alias($_GET['q']);
    if ($alias != $_GET['q']) {
        $template_filename = 'page';
        foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '-' . $path_part;
            $vars['template_files'][] = $template_filename;
        }
    }
    //---- 
}

【问题讨论】:

    标签: templates drupal blogs


    【解决方案1】:

    假设您的内容类型称为“博客”,那么每当需要显示博客文章时都会使用 node-blog.tpl.php。如果 Drupal 想要显示预告片,则 node-blog.tpl.php 中的 $teaser 变量将设置为 TRUE,如果节点以全页视图显示(它们都将是如果完整节点显示在节点列表中,则为 FALSE)。因此,您需要设置您的 node-blog.tpl.php 以检查请求的显示类型并返回适合给定类型的 HTML。您的 node-blog.tpl.php 的一般设置应遵循以下原则:

    if($teaser){
      //return teaser html
    }
    else{
      //return full node HTML
    }
    

    您的问题对我来说有点不清楚,但听起来您可能在 node-blog.tpl.php 中有某种循环代码来迭代您网站上的节点。你不想这样做。 Drupal 不像 Wordpress 那样工作。

    您没有提及在 /blogs/eric 中如何生成预告片列表,但我建议使用 Views 模块。如果您使用 Views 生成预告片列表,那么您将能够使用 Views 的主题轻松地为列表设置主题。

    在您添加示例代码后已编辑 要将任意 HTML 仅粘贴在博客页面的完整节点显示的顶部,您可以编辑 node-blog.tpl.php 使其看起来像这样:

    <?php if ($page): ?>
    My arbitrary HTML here which will not show up in teasers and only at the top of full blog pages
    <?php endif; ?>
    
    <div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
    <div class="item">
    <ul>
    <?php print $picture ?>
    
    <?php if ($page == 0): ?>
    <?php endif; ?>
    
      <div class="content clear-block">
    
        <li class="title"><h4><?php print $title ?></h4></li>
        <li class="photo"><a href="#"><img src="/<?php print $node->field_blog_image[0]['filepath']; ?>" /></a></li>
        <li class="desc"><?php print $node->content['body']['#value']; ?></li>
        <li class="link">
        <?php if ($teaser): ?>
        <a href="<?php print $node_url ?>" class="block-link">Read more</a> | <a href="<?php print $node_url ?>" class="block-link">Audio/Video</a> | 
        <?php endif; ?>
        <?php print $submitted; ?>
        </li>
        <div class="clear"></div>     
    
      </div>
    
      <div class="clear-block">
        <div class="meta">
        <?php if ($taxonomy): ?>
          <div class="terms"><?php print $terms ?></div>
        <?php endif;?>
        </div>
    
      </div>
    </ul>
    </div>
    </div>
    

    在发现您正在使用博客模块后进行了编辑 要在博客预告列表的顶部显示任意 HTML,只需将其粘贴在 page-blog.tpl.php 的顶部

    【讨论】:

    • 谢谢亚伦。我没有循环任何东西,我想说我注意到 node-blog.tpl.php 正在用于每个节点预告片。因此,在 /blogs/eric 页面上,似乎每个预告片都在调用该 tpl 文件。我想要的是每个博客页面上方不重复的标准标题。我想要这个用于博客预告页面和博客完整页面。感谢您的帮助
    • /blogs/eric 中显示博客条目列表的内容是什么?是视图还是提供该页面的其他模块?
    • 对于 /blogs/eric 我有 page-blogs.tpl.php 作为我的主要模板,它正在打印 。我在我的原始帖子中粘贴了我的 node-blog.tpl.php 文件中的内容。我没有为此使用任何视图。
    • 啊,你在用博客模块吗?
    • 是的,我是。我确实使用博客的 Pathauto 设置修改了 URL。但没有别的。
    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多