• I’m totally new to WordPress & PHP so please excuse me if I’ve made silly mistakes!

    I have created a child theme using twentyten as the parent theme but I’m having problems getting the custom fields to work. I have added in a custom field and it’s value within the admin area and have added this piece of code:

    <div id=”tracker” class=”float_right”>
    <?php echo get_post_meta($post->ID, ‘miles_cycled’, true ); ?> miles cycled
    </div>

    But it doesn’t work! Is there anything glaringly obvious that I’m missing?

    Thanks for any help you might be able to offer!

Viewing 5 replies - 1 through 5 (of 5 total)
  • There is nothing obvious in the code you showed.

    Is this code ‘in the Loop’ where the post ID is available?

    Is ‘miles_cycled’ the correct field name? Could it be ‘miles-cycled’, or ‘Miles_cycled’ for example?

    Does anything at all show? Do you get ‘ miles cycled’ with no value before it? If not, maybe the code is not being executed.

    Thread Starter daisy-d

    (@daisy-d)

    Thanks for your reply.

    I’ve tried a couple of different things – I’ve tried adding a loop (not entirely sure that I did it correctly!), without the loop and also using this piece of code (with no loop):

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

    I’ve definitely got the correct field name in there too. Yes, that’s right – miles cycled shows up with no value before it.

    Thread Starter daisy-d

    (@daisy-d)

    I have been creating the home page for the site so I was using a page rather than a post – once I created a post the code started working!

    Is there any way to get it working for a page?

    It works exactly the same way in Pages.

    Thread Starter daisy-d

    (@daisy-d)

    I’ve got it working on the page now but I have no idea what I did that was any different

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Can't get Custom Fields to work’ is closed to new replies.