tags:

views:

229

answers:

3

Hi, i'm a wordpress user and i have a php code, and in that php code there is an area to put a url in: $url = "http://blabla.com"; well in wordpress you can call post permalinks with this code: <?php the_permalink(); ?> What i want to do is putting <?php the_permalink(); ?> instead of http://blabla.com above in the php code. Target is: getting permalinks put them there in php code, and let the php code use them to do its job. Is that possible? If yes, how with an example please...Thank you...

+4  A: 

You could assign the return value of get_permalink() to $url:

<?php $url = get_permalink(); ?>

get_permalink() is different from the_permalink() because it doesn't display the link, but just returns it. (Originally this answer naively used the_permalink(), but I did some extra research to be sure.)

Twisol
+2  A: 

The above answer will not work. Use get_permalink().

the_permalink will display it's output

get_permalink will return the value.

<?php $url = get_permalink(); ?>
Littlejon
Sorry, posted this before Twisol updated his answer :-)
Littlejon
Haha, no worries. (And 'the above answer' isn't reliable here on SO, because answers can be sorted multiple ways, the default being by votes rather than chronologically)
Twisol
No, still not working.
You may need to add the post_id into the get_permalink($post_id) call. See http://codex.wordpress.org/Template_Tags/get_permalink
Littlejon
A: 

You need to understand the concept of some Template Tags, since the_permalink fits this category. They are defined especially for use in WordPress Themes. They can be summarize as "a code that instructs WordPress to "do" or "get" something".

Writing the_permalink simply echoes the permalink in your template. It's not something that you get in a php function and manipulates it. It just echoes the html of the information it needs to show.

This is useful for designers working out template files in Wordpress themes: they don't need to understand a lot of programming or a lot of php keywords: they just need to know that writing "the_permalink" gives them the desired html output.

What you're trying to do is get the output from a template tag that already outputs it's value. You need to use other template tag that RETURNS the value you want to use instead of ones that OUTPUTs it.

In your example, you need the get_permalink. Since the_permalink is used in the Loop, you need to provide a post id to your get_permalink function.

There are another examples that fits the same problem domain: for example, I can't manipulate what wp_list_pages returns (because it automatically outputs it's result), so I need get_pages (that RETURNS an array) to manipulate it's result.

Read Wordpress official documentation at Codex. It's great.

GmonC