Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 2 • The WordPress Syntax 33


PASSING MULTIPLE PARAMETERS TO A TEMPLATE TAG


Outputting content with template tags is easy enough. Some won’t take parameters at all, and
others, like bloginfo(), will just take one. Others, however, will take several parameters.


Two really useful template tags for bloggers are edit_post_link() and editcomment
link(). They basically do the same thing, which is to add an edit link to posts or comments
so that you (when logged in as a user having the necessary credentials) can fix errors quickly
by clicking the edit link: This will take you to the admin interface where you can correct your
error or censor that particularly nasty (but most likely valid) comment. These tags are
especially handy if you have disabled the top admin bar.


To use these tags, put them in your theme’s file along with the posts/comments. Both tags
work the same way. The tags need to be within the loop, which is discussed in the next
chapter, but for now, all you need to know is that edit_post_link() goes with the code
that outputs the posts, and similarly, edit_comment_link() goes with the code that
outputs the comments.


For example, this is how edit_post_link() looks when passing its default parameters:


<?php edit_post_link(); ?>


If you put that code in your theme, you’ll get a link that says “Edit This” wherever you put the
code, and that’s it. Suppose you want this link to show up in its own paragraph, say “Admin”
before the actual link, and say “Edit this post” rather than the “Edit This” default. Simple —
just use this instead:


<?php edit_post_link( 'Edit this post', '

Admin: ', '

' ); ?>


edit_post_link() supports four parameters. The first one is the link text, 'Edit this
post' in this case, and the second is what goes before the link. Remember, you wanted a separate
paragraph for the edit link, and you wanted it to say “Admin” in front, so here’s '

Admin: '.
(Note the blank space after the text to make some room in front of the link.) The third parameter
is what goes after the link, which is just '

' because you need to close the

tag. Finally,
the fourth parameter is $id, which would be the post ID, and is omitted here.


In other words, edit_post_link() can handle four parameters. You pass them like this,
to speak a little PHP:


<?php edit_post_link( $link, $before, $after, $id ); ?>


Remember, parameters are usually passed within quotation marks and separated with
commas and a space to make them more readable.


Not all that complicated, right? All you need to know is which parameters there are to pass
and in what order they need to be. The order is important: Without the correct order, you
might get the wrong text linked and would definitely break your design, or at least the
validation of the site.

Free download pdf