Posted by vfranklyn on March 14, 2011 at 10:05pm
Hi there,
I'm wondering if anyone out there can help out a php newbie. I'm trying to add a snippet of code to the bottom of a tpl.php file for a block view. The code I'm trying to add is this:
<p>
Click the title of this post to view comments.
</p>
<p>
<a href="http://ppld.org/comment/reply/5865#comment-form">Add a new comment</a>
</p>
where "5865" is the node id. I don't know how to make that a variable in php on a tpl.php file. Any help is greatly appreciated!
Comments
$nid is probably what you're
$nid is probably what you're looking for.
In a node.tpl.php file, the following code should work for you:
<a href="http://ppld.org/comment/reply/<?php print($nid); ?>#comment-form">Add a new comment</a>
I hope this helps.
-Winn
Hi Winn, I tried your
Hi Winn,
I tried your solution and the variable comes up blank. Do I need to define $nid, do you think? If so, what's the correct syntax for that?
Maybe because it's not the node.tpl.php file that I'm altering, it's views-view-unformatted--homeschool--block-1.tpl.php.
Thank you so much for your time!
You are probably going to
You are probably going to have to add something like this to your preprocess function in your theme's template.php and then make sure this block only displays on nodes.
function mytheme_preprocess_views_view_unformatted__homeschool(&$vars) {
if (arg(0) == "node" && is_numeric(arg(1))) {
$vars['nid'] = arg(1);
}
else {
// You could put a default here in case your block displays somewhere you don't expect.
$vars['nid'] = '';
}
}
Make sure you replace mytheme with your theme's name and be sure to clear all your caches. I may not have picked the correct function name so you might need to double check it.
Thanks Halstead, I'll give it
Thanks Halstead, I'll give it a try.
The "down and dirty" way
The "down and dirty" way would be:
<?php $node = node_load(arg(1)); ?>
<a href="http://ppld.org/comment/reply/<?php print $node->nid; ?>#comment-form">Add a new comment</a>
You would probably want to wrap this in a sanity check, to make sure that the block is on a node.
Hi Highermath, Thanks so much
Hi Highermath,
Thanks so much for your time. It pulled the node id for the page the block was inserted into, but I need it to pull the node id for each blog entry that is pulled into the view and displayed on the page. I hope this makes sense. Here's a link to the actual page:
http://ppld.org/homeschool-hub
You'll see some blog entries below the main blurb. I'm trying to add a link to 'add a new comment' on each of those entries.
That is pretty easy to do in
That is pretty easy to do in the view. Just add a field:
If the node ID (nid) is already available, add a Global: custom text field with
<a href="http://ppld.org/comment/reply/[nid]#comment-form">Add a new comment</a>
If it is not, add a Node: nid field and rewrite the text as above.
If you want the comment link
If you want the comment link to display for each node loaded you should use the views interface to do this. Edit your view and add a new field from the node group. Select Node: Nid and then check "Rewrite the output of this field". In the text box put
"<a href="http://ppld.org/comment/reply/[nid]#comment-form">Add a new comment</a>"
and update the view. Save it and you should be very close to what you want. Tweak the label and text as you need to.Edit: added code tags.
Highermath and Halstead, you
Highermath and Halstead, you both ROCK! That was exactly what I needed to do. Thank you so so so much!!! I really am so grateful for your time and help. Yay!!