WP Gurus - Help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madtwin
    Confirmed User
    • Aug 2009
    • 274

    #1

    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?
  • scouser
    marketer.
    • Aug 2006
    • 2280

    #2
    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)

    Comment

    • fris
      Too lazy to set a custom title
      • Aug 2002
      • 55679

      #3
      Originally posted by deadmoon
      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.
      Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.

      Comment

      • madtwin
        Confirmed User
        • Aug 2009
        • 274

        #4
        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)

        Comment

        Working...