Support » Fixing WordPress » Custom Fields

  • mingis

    (@mingis)



    I have numerous custom fields working on different pages of my blog, but for some reason on one page they just won’t work!

    All I am trying to do is display an image using the following code:

    <img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" width="250" height="120" alt="thumb"/>

    And a backgroud image using:

    <div id="portfolio-feature" style="background:#000 url(<?php $key="background"; echo get_post_meta($post->ID, $key, true); ?>) center center;"></div>

    They are both within the loop and if I use <?php the_meta(); ?> I can see that the key values are returned fine, but for some reason they just don’t work in the img src

    Would be most grateful for any help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • s_ha_dum

    (@apljdi)

    URL?

    mingis

    (@mingis)

    Hi Sorry it’s on a local server so I don’t have a URL, but here is the code for the page, would be most grateful for any ideas:

    <?php
    /*
    Template Name: Portfolio-detail
    */
    ?>
    <?php
    require 'header-portfolio.php';
    ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <div id="portfolio-feature" style="background:#000 url(<?php $key="background"; echo get_post_meta($post->ID, $key, true); ?>) center center;">
    
    <div id="slider">
    <?php the_content('') ?>
    <img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" width="250" height="120" alt="thumb"/>
    </div>
    
    </div>
    
    <div id="content-wrapper">
    
    <div id="content">
    
    <div class="content-left portfolio-page">
    
    <div id="buttons">
            <a href="#" id="btn-prev"></a>
            <a href="#" id="btn-pause"></a>
            <a href="#" id="btn-play"></a>
            <a href="#" id="btn-next"></a>
    </div>
    
    <h1><?php the_title('') ?></h1>
    
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    
    </div>
    
    </div>
    
    <?php get_footer(); ?>
    s_ha_dum

    (@apljdi)

    Sorry it’s on a local server so I don’t have a URL…

    Fair enough… and the only acceptable excuse. ☺

    I don’t see any reason to set the $key variable. I’d just write the key into the function and I’d put quotes around the url() string like so: style="background:url('<?php echo get_post_meta($post->ID, "background", true); ?>'). Minor points though. I don’t see why your code is failing. Load the page, view-source, and see if everything looks right. You might check permissions on the images too.

    Thanks apljdi – I tried that, but no joy.

    However I think I’m beggining to see the source of the problem: Does WordPress treat template pages differently?

    I’m trying to achieve this on a post that has a template assigned to it so I detached the post from the template and added the custom field code to single.php

    style="background:#000 url(<?php echo get_post_meta('<?php echo get_post_meta($post->ID, "background", true); ?>'); ?>

    when I reload the page the post then loaded from the default single.php and the background image works fine.

    Soooo…

    I copied all the code from single.php into my template (so the two files were exactly the same) and then re-assigned it to the post – I reloaded and . . . . it’s gone!

    The code works for me, though of course I had to use a meta_key that exists in my install. What exactly does your echo get_post_meta statement return? And why do you have nested get_post_meta’s?

    Does WordPress treat template pages differently?

    Well, yes… but not in a way that should effect this. How are you ‘assigning’ the post to the page?

    Jeez sorry – I posted that code incorrectly, I didn’t mean to nest anything (this is frying my brain!).

    I meant to post:

    style="background:url('<?php echo get_post_meta($post->ID, "background", true); ?>')

    i.e. the original recommendation by yourself!

    My get_post_meta statement returns nothing – the background url is just blank! (except the single quote marks).

    However if I use the <?php the_meta(); ?> function it returns the two custom fields exactly as I would expect them to look.

    My get_post_meta statement returns nothing – the background url is just blank! (except the single quote marks).

    Ok. That is getting somewhere. Either $post->ID is empty or there is no meta_key = “background” in the database associated with the post. Echo $post->ID by itself to see what happens.

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