• Hi, All

    I have a question regarding the custom fields function when creating a Page.

    I have created a custom template page.

    This cointains:

    <?php /* Template Name: Digital_Log */
    get_header(); ?>
    	<html>
    		<iframe src="/logbook/log.php" width="100%" height="6500" style="border:none" scrolling="no";></iframe>
    	</html>
    <?php get_footer(); ?>

    This works, I get the header, footer and the redirected page in the middle of it.

    Now I want to create a Custom Field named: “url-name”
    The value of the field is: “/logbook/log.php”

    I want to pass this to a variable in to the custom template file in PHP.

    I have tried this example:
    <?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>
    Where $key = “url-name”

    But I get nothing.

    Any Advice would be apreciated.

    Bjorn

    • This topic was modified 8 years, 2 months ago by pd5dj.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Well, your code says $key = “mykey”, not “url-name”. Easily fixed. Your other problem is probably $post->ID. $post is often not defined within the template’s scope. Generally, declaring global $post; fixes the scoping issue, but I think even better when you need a post ID is to use get_the_ID(). It works out all the vagaries of determining the post ID for you. I assume you wish to get the return value from get_post_meta() into the iframe src attribute. So in summary, try this:

    <?php $key="url-name";
    $url = get_post_meta( get_the_ID(), $key, true);
    ?>
    <iframe src="<?php echo &url; ?>" width="100%" height="6500" style="border:none" scrolling="no";></iframe>
Viewing 1 replies (of 1 total)

The topic ‘Custom Fields to variable in PHP’ is closed to new replies.