WordPress.org

Support

Support » How-To and Troubleshooting » Custom Field Help

Custom Field Help

  • Hello,

    I would like to display a custom field in my footer.php file, I’ve managed to display the custom field in the main page using the code below which I want, but I’d also like to show the custom field in the footer, which would result in the custom field value being displayed in all pages of my theme, which is my goal.

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'Your-Custom-Field', true);
    wp_reset_query();
    ?>

    [Please post code snippets between backticks or use the code button.]

    Is this possible? If anyone could provide me with a solution that would be brilliant.

    Thanks in advance
    Mark

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello,

    I would like to display a custom field in my footer.php file in wordpress, I’ve managed to display the custom field in the main page using the code below which I want, but I’d also like to show the custom field in the footer, which would result in the custom field value for this page being displayed in all pages of my theme, which is my goal.

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'Your-Custom-Field', true);
    wp_reset_query();
    ?>

    Is this possible? If anyone could provide me with a solution that would be brilliant.

    Thanks in advance
    Mark

    What do you want to do with the field… Can we see the website?

    Hi Jimmy, thanks for replying. The website isnt live yet and I’m building it on my local server so can’t show you. I’ll try build a picture of what I want to give you a better understanding.

    I have a 5 page website for a business (including a testimonial page). I wanted to display their 5 latest testimonials on each page that is created. So I thought placing them in the footer would be perfect. The fields would be used to display the current values, which will be the latest testimonials.

    However the client wants to be able to update their website including testimonials. I want the client to be able to edit the 5 latest testimonials in the custom fields from their testimonial page, and if they wish they can add more in the main page which are only viewable there.

    I hope this makes it a little clearer. If theres a more simpler way of achieving this, I’m open to suggestions.

    Thanks again
    Mark

    Try adding something like this to your footer.php file:

    <?php
    global $post;
    if( is_object( $post ) ) {
      echo get_post_meta($post->ID, 'Your-Custom-Field', true);
    }
    ?>

    Hi Curtiss,

    That displays in the footer on the ‘testimonials’ page, which is a good start and is what I’m aiming for but for some reason does not show up on any of the other pages in the footer? Is there anything else I can try? Surely its doable?

    Cheers
    Mark

    So, what you want to do is get the “testimonials” custom field from the latest 5 posts?

    I would think you need a whole new loop:

    <?php query_posts("showposts=5&meta_key=Your-Custom-Field"); ?>
      <?php while (have_posts()) : the_post();	
    
            echo get_post_meta($post->ID, 'Your-Custom-Field', true); ?>
    
      <?php endwhile; ?>

    Hi coopersita,

    I’m using static pages rather than posts. I would like 5 separate custom fields, I’ve got my own page template for the testimonials, so I’d have 6 separate loops. i.e…

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'testimonial1', true);
    wp_reset_query();
    ?>
    
    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'testimonial2', true);
    wp_reset_query();
    ?>

    plus the other 3 custom fields. I’d also have the main loop which will be used to show the main page with any additional testimonials added.

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php the_content(__('(more...)')); ?>
    <?php endwhile; ?>
    <?php endif; ?>

    Your code does look easier, but I’ve not really worked with posts yet, so wouldn’t know where to begin. Plus I wouldnt want people to be able to comment on them, or see when they are posted etc…

    I would then like to repeat the custom field loops in the footer.php to show throughout the site. Hope all this makes sense.

    Thanks again

    Try this:

    <?php query_posts("showposts=5&meta_key=Your-Custom-Field&post_type=page"); ?>
      <?php while (have_posts()) : the_post();	
    
            echo get_post_meta($post->ID, 'Your-Custom-Field', true); ?>
    
      <?php endwhile; ?>

    The problem is that you have this:$postid = $wp_query->post->ID;, but where is the post->ID coming from? How does id know which pages to bring in?

    That’s why you need to have a loop that calls for the latest 5 (or whatever number) pages, with a custom post type of ‘Your-Custom-Field”. Otherwise it will use the post ID of the posts that were brought in by that page’s loop.

    Yes coppersita, your a genius, this works perfectly, thank you so much.

    I changed showposts=5 to showposts=0 and it still works, should I still use the amount of pages anyway?

    Thanks for your help…

    How would you make something like this linkable? What if my custom field is a URL and I’m calling it such as:

    <?php
    global $post;
    if( is_object( $post ) ) {
      echo get_post_meta($post->ID, 'Your-Custom-Field', true);
    }
    ?>

    How would you link that as a hyperlink?

    Just add this round the text in WP

    <a href="www.targetdomain.com" target="_blank">Your Value</a>

    I’m sure theres a way you can add it to the PHP but this way you can change the target address from WP.

    Marques_uk

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Custom Field Help’ is closed to new replies.