GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   WP Gurus - Help needed (https://gfy.com/showthread.php?t=1056875)

madtwin 02-09-2012 06:13 AM

WP Gurus - Help needed
 
I have a custom field called XYZ with url and I need to put that url in theme template (footer), outside the loop - like this: <a href="XYZ value">some example text</a>

When I call this custom field:

<a href="<?php echo get_post_meta($post->ID, 'XYZ', true); ?>">some example text</a>

it just shows <a href="current posts permalink">some example text</a>


So how to do that properly?
:mad:

scouser 02-09-2012 06:30 AM

if u need to do it outside the loop with post->id u need to do the_post() - you doing that? it shouldn't be showing the permalink though in any case. sure the XYZ value is set up correctly? inside the loop does it show the right value?

if u use the post inside the loop (and always will) why not just do this inside the loop

$footerData = get_post_meta(get_the_ID(), 'XYZ', true);
(get_the_ID() can be changed with $post->ID...)

then in the footer just <?=$footerData;?> it

(not checked any code... might be errors)

fris 02-09-2012 07:22 AM

Quote:

Originally Posted by deadmoon (Post 18747416)
if u need to do it outside the loop with post->id u need to do the_post() - you doing that? it shouldn't be showing the permalink though in any case. sure the XYZ value is set up correctly? inside the loop does it show the right value?

if u use the post inside the loop (and always will) why not just do this inside the loop

$footerData = get_post_meta(get_the_ID(), 'XYZ', true);
(get_the_ID() can be changed with $post->ID...)

then in the footer just <?=$footerData;?> it

(not checked any code... might be errors)

get the id is only for the loop.

you will need to pass the actual post id outside the loop for the footer.

and if you are changing it each post, i would store the postid in a variable then pass that on to the footer code.

madtwin 02-09-2012 11:38 AM

Ok guys, thank a lot for the info.

I found the solution, maybe someone will find it useful too:

Displaying Custom Fields Outside The Loop

There are times, when you need to display custom fields outside the loop, e.g., in the footer of the blog post. Displaying custom fields outside the loop will allow you, for example, to have a different footer for each blog post. To achieve, this two things need to be done.

1. Edit the blog post template (e.g., single.php) to make post ID available outside the loop. Find the line

Code:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
and add the following code just below it:

Code:

<?php $GLOBALS['current_id'] = $post->ID; ?>
2. Use this code to display your custom field outside the loop, e.g., in the footer.php

Code:

<?php echo get_post_meta($GLOBALS['current_id'], "custom-field-name", true); ?>
That?s it! Now, when editing a blog post, add a custom field with ?custom-field-name? and it will display wherever you put that second bit of code.

source (through google cache)


All times are GMT -7. The time now is 02:15 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc