1

Drupal 7 でサブテーマを作成し、残りのコンテンツが通常ある場所の外にあるカスタム コンテンツ タイプからpage.tpl.php値 (プレーン テキスト) を取得する必要があります。field_EXAMPLE

<!-- Adding $title as normal-->
    <?php print render($title_prefix); ?>
        <?php if (!empty($title)): ?>
            <h1><?php print $title; ?></h1>
        <?php endif; ?>
    <?php print render($title_suffix); ?>

<!-- THE ISSUE: Adding field_EXAMPLE -->
    <h2> <?php print render($field_EXAMPLE;);?> </h2>
    ...
<!-- Where the rest of the content loads by default -->
    <div><?php print render($page['content']); ?></div>

うまくいくでしょうかfield_get_items

function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_EXAMPLE}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}

それともこれ?

$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];

これ入れpage.tpl.phpますか?それらを試してみましたが、サイコロはありません。-初心者

これが var_dump(get_defined_vars()); です。

              ["field_thestring"]=>
              array(1) {
                ["und"]=>
                array(1) {
                  [0]=>
                  array(3) {
                    ["value"]=>
                    string(44) "This is a string of text please refer to me "
                    ["format"]=>
                    NULL
                    ["safe_value"]=>
                    string(44) "This is a string of text please refer to me "
                  }
                }
              }
4

4 に答える 4

1

field_thestringページのコンテンツがレンダリングされる の外側の の外側の場所で、コンテンツ タイプarticleのページにレンダリングするという名前のフィールドを作成したと仮定しますTHEME

ステップ 1. テーマの page.tpl.php をコピーします。名前を変更しpage--article.tpl.phpます。

ステップ 2. ではpage.var.php

function THEME_preprocess_page(&$variables) {

// To activate page--article.tpl.php 
if (isset($variables['node']->type)) {
 $nodetype = $variables['node']->type;
 $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;    
}

// Prevent errors on other pages
if ($node = menu_get_object()) {

if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}

ステップ 3.page--article.tpl.php追加<?php print render($thestring); ?>

最初は、すべてのコンテンツ タイプにタイトルがあるため、すべてのコンテンツ タイプに別のフィールドを持たせたいと考えていました。これは、さらなる開発に適したアイデアではないと判断しました。

ソース

于 2016-10-26T13:24:34.920 に答える
0

コードをページ テンプレートに直接配置することもできますが、ページの前処理フックに配置し、テンプレート変数を設定して、ページ テンプレートからその変数を使用することもできます。

https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

それはちょっときれいな方法ですが、両方ともうまくいくはずです。

また、フロントエンドで変更がすぐに表示されない場合は、Drupal のキャッシュを削除してみてください。

ノードIDを取得するには、次を試してください:

global $node;
$nid = $node->nid;
$node = node_load($nid);
...

それがうまくいかない場合は、これを試してください:

    if ($node = menu_get_object()) {
      // Get the nid
      $nid = $node->nid;
      $node = node_load($nid);
      ...
    }

またはこの方法:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);
  $node = node_load($nid);
  ...
}
于 2016-10-25T08:55:47.083 に答える
0

前処理を使用して、テンプレートに渡される $variables に値を追加できます

https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

template.php で:

MYTHEMENAME_preprocess_page(&variable){
 $values = current(field_get_items('node', $variable['node'],'field_machine_name'));
 $variable['myvar'] = $values['value'];
}

あなたの template.tpl.php で

echo $myvar; // contains your value
于 2016-10-25T10:07:15.410 に答える