【问题标题】:create node wuth Term reference tag创建节点 wuth 术语参考标签
【发布时间】:2015-10-18 11:34:27
【问题描述】:

我正在尝试使用术语引用以编程方式添加节点自动完成术语小部件(标记)并添加新标记我的代码不起作用

当我从代码中删除标签引用时,我的节点添加成功 我需要为每个节点添加新标签

 <?php

       $bodytext = "body?";

      $node = new stdClass(); // Create a new node object
       $node->type = "programstvradio"; // Or page, or whatever content type you              like
node_object_prepare($node); // Set some default values
// If you update an existing node instead of creating a new one,
// comment out the three lines above and uncomment the following:
// $node = node_load($nid); // ...where $nid is the node id
 $node->title    = "title";
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
 $node->uid = 1; // UID of the author of the node; or use $node->name
 $node->body[$node->language][0]['value']   = $bodytext;
$node->body[$node->language][0]['format']  = 'filtered_html';
 // I prefer using pathauto, which would override the below path
$path = 'node_created_on' . date('YmdHis');
$node->path = array('alias' => $path);

$node->field_tag_[$node->language][]['value'] ='tagggggggg';
 $node->field_catprogram[$node->language][]['tid'] =12;



$file_path = drupal_realpath('C:\Users\Admin\Desktop\drupal.png'); // Create a File object'); // Create a File object
$file = (object) array(
  'uid' => 1,
  'uri' => $file_path,
  'filemime' => file_get_mimetype($file_path),
  'status' => 1,
); 
$file = file_copy($file, 'public://images'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images' 
$node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:

if($node = node_submit($node)) { // Prepare node for saving
    node_save($node);
    echo "Node with nid " . $node->nid . " saved!\n";
}
?>``

【问题讨论】:

    标签: database drupal-7


    【解决方案1】:

    我找到解决办法

    $my_term_name = 'gggg';
    $term_array = taxonomy_get_term_by_name($my_term_name);
    if($term_array == array()){
        //empty term ..
        $term->name = $my_term_name;
        $term->vid = 1;
    taxonomy_term_save($term);
    $term_array = taxonomy_get_term_by_name($my_term_name); 
    }
    //get the first index of the array .
    foreach ($term_array as $tid => $term_object)break;
    $node->field_progna['und'][$tid] = (array)$term_object;
    

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      相关资源
      最近更新 更多