snipt

Ctrl+h for KB shortcuts

PHP

Load author of node and render custom fields from user profile in D7

1
2
3
4
5
6
<?php
// load author
// user this inside of custom node template, e.g. node--blog.tpl.php
$author = user_load($node->uid);
?>
<div class="blog-author-name"><h3>Posted by <?php print $author->field_user_first_name['und'][0]['value'].' '.$author->field_user_last_name['und'][0]['value']; ?></h3></div>
https://snipt.net/embed/39847a535de37041f0e17950e8e00d8e/
/raw/39847a535de37041f0e17950e8e00d8e/
39847a535de37041f0e17950e8e00d8e
php
PHP
6
2019-08-20T16:44:00
True
False
False
/api/public/snipt/35625/
load-author-of-node-and-render-custom-fields-from-user-profile-in-d7
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// load author</span> </span><span id="L-3"><a name="L-3"></a><span class="c1">// user this inside of custom node template, e.g. node--blog.tpl.php</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$author</span> <span class="o">=</span> <span class="nx">user_load</span><span class="p">(</span><span class="nv">$node</span><span class="o">-&gt;</span><span class="na">uid</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-6"><a name="L-6"></a><span class="x">&lt;div class=&quot;blog-author-name&quot;&gt;&lt;h3&gt;Posted by </span><span class="cp">&lt;?php</span> <span class="k">print</span> <span class="nv">$author</span><span class="o">-&gt;</span><span class="na">field_user_first_name</span><span class="p">[</span><span class="s1">&#39;und&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">]</span><span class="o">.</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="nv">$author</span><span class="o">-&gt;</span><span class="na">field_user_last_name</span><span class="p">[</span><span class="s1">&#39;und&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">];</span> <span class="cp">?&gt;</span><span class="x">&lt;/h3&gt;&lt;/div&gt;</span> </span></pre></div> </td></tr></table>
"drupal 7", "user profile", author, node