Hello,
What I want to achieve is pretty much common but I am missing out something silly that I cant get what I needed. Any help is greatly appreciated.

I wanted to have a node->title field in my views page and link it to the respective node page. I can add that field easily to my views fields section. But the problem is the generated html is
<a href='someurl'>TITLE</a>

while I want it something like
<h2><a href='someurl'>TITLE</a></h2>

Note the addition h2 above.

If I use the rewrite output option in views to override the default output of Title field, I get
<a href='someurl'><h2>TITLE</h2></a>

Note h2 element is inside <a> tag. which is also not what I needed. I do not want the block level element to be inside an inline element.

so my latest approach is that to have a NID field in views (instead of a title field) and use a custom views template file for views-view-field--nid.tpl.php.

$nid = $output;
$node = node_load($nid);
$node_url = drupal_get_path_alias('node/' . $nid);
$node_title = '<a href="' . $node_url . '">' . $node->title . '</a>';
print '<h2>' . $node_title . '</h2>';

Above is my code in views-view-field--nid.tpl.php and the output is <h2><a href='someurl'>TITLE</a></h2> as I expected. But then there is now a new problem. I get a <span> tag surrounding my <h2>

So now question at last is:

How do I tell Views that my NID field is a block level field and not a inline field?

Does this make sense or am I making something silly very complicated?

thanks,
neokrish

Comments

WorldFallz’s picture

Why not just use the 'rewrite the output of this field' option right on the node title field in the view?

neokrish’s picture

I have already tried that solution and see my note below:

If I use the rewrite output option in views to override the default output of Title field, I get
<a href='someurl'><h2>TITLE</h2></a>

Note h2 element is inside <a> tag. which is also not what I needed. I do not want the block level element to be inside an inline element.

WorldFallz’s picture

Sorry I missed that sentence-- but I don't get that output at all. I get exactly whatever I enter into the text box in the rewrite option. If i enter <h2><a href="someurl">[title]</a></h2> that's what I get-- the h2 tags are not moved.

And if the span tag bothers you, then yes you need to use a template file and theme the field as desired.

tentonjim’s picture

Hi,
I am having the same issue as neokrish - rewriting the output of title field as
<h3>[title]</h3>
and checking the "link to node" box. Yep the h3 is nested inside the <a> .

How can you - is there a way to - assign the $node_url variable inside the rewrite box? I tried it and no go but maybe someone knows a way to do it there?

I've tried various ways - guessing... for example:
<h3><a href="[$node_url]">[title]</a></h3>

It's a view so I don't know or don't want to hard code urls for all the titles there are hundreds.
Thanks!
Jim

tentonjim’s picture

only way I got it to work.
I took out the "rewrite" stuff, and made a custom title field view template...
views-view-field--blog--title.tpl.php

within that file...
<h3><?php print $output; ?></h3>

checked the "link to node" box from within the view for the title field - unchecked "rewrite output".

That put the H3 outside the anchor tags, nested correctly.
Not ideal but - gotsta do what ya gotsta do.
Thanks!
Jim

WorldFallz’s picture

Though it should work properly without a rewrite, another way to do it without needing a template file is to add the "Node: nid" field, make sure it appears before the title field, and check the 'exclude from display' option. Then set the rewrite to <h3><a href="node/[nid]">[title]</a></h3>