4

私はここにこのコードでノードにuserpictureを表示しています:

<?php
    $user_load = user_load($node->uid);
    $imgtag = theme('imagecache', 'avatar_node', $user_load->picture, $user_load->name, $user_load->name);
    $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
    print l($imgtag, 'u/'.$user_load->name, $attributes);
?>

これは、ユーザーが写真を持っていない場合を除いて、うまく機能します。写真がない場合は、奇妙に見えます。

ユーザーが画像を持っていない場合、デフォルトの画像をロードするにはどうすればよいですか。このブロックで$pictureにアクセスできるとは思いません。

前もって感謝します。

4

3 に答える 3

6

    $user_load = user_load($node->uid);
    $picture = $user_load->picture ? $user_load->picture : variable_get('user_picture_default', ''); // 
    $imgtag = theme('imagecache', 'avatar_node', $picture, $user_load->name, $user_load->name); 
    $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
    print l($imgtag, 'u/'.$user_load->name, $attributes);

デフォルトの画像をファイルディレクトリにコピーする場合は、http://api.drupal.org/api/function/file_directory_path/6から確認できます。

于 2010-01-11T08:14:12.557 に答える
2

これは私が同様の状況で使用しているものです:

      if (!empty($user->picture) && file_exists($user->picture)) {
        $picture = file_create_url($user->picture);
      }
      else if (variable_get('user_picture_default', '')) {
        $picture = variable_get('user_picture_default', '');
      }

      if (isset($picture)) {

        $alt = t("@user's picture", array('@user' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous'))));
        $picture = theme('image', $picture, $alt, $alt, '', FALSE);
        if (!empty($user->uid) && user_access('access user profiles')) {
          $attributes = array(
            'attributes' => array('title' => t('View user profile.')),
            'html' => TRUE,
          );
          echo l($picture, "user/$user->uid", $attributes);
        }
      }

これは、http://api.drupal.org/api/drupal/modules%21user%21user.module/function/template_preprocess_user_picture/6から採用されています。

于 2013-04-15T00:24:46.843 に答える
1

Drupal 7では、次のものを使用します。

global $user;
file_create_url( file_load($user->picture)->uri)
于 2013-08-16T15:31:41.803 に答える