Grow your CSS skills. Land your dream job.

[Solved] Custom Fields get_post_meta question

  • # September 24, 2009 at 6:12 pm

    I am creating a post template in my wordpress.

    I need to have:
    1/ a dynamic src inside an image – so I can place "artist-image" as custom field and the path. This works.
    2/ I need to a link underneath that contains a website link
    3/ pretty much the same as number 2

    Here’s my PHP – 2 and 3 aren’t working – but number 1 is. What am I doing wrong? Any ideas?

    <!– I NEED TO POST AN IMAGE FROM THE IMG and THEN TWO SEPERATE LINKS BELOW – 1/ WEBSITE 2/ MYSPACE URL –>

    <div class="artist-details">
    <?php
    $artistimageCode = get_post_meta ($post ->ID, ‘artist-image’, true);
    $websiteCode = get_post_meta ($post ->ID, ‘website-address’, true);
    $myspaceCode = get_post_meta ($post ->ID, ‘myspace’, true);
    ?>

    <img src="<?php echo $artistimageCode; ?>" width="200" height="250" alt="music box for life artist" />
    <p>Website: <a href="<?php echo $websiteCode; ?>" target="_blank"></a></p>
    <p>Myspace: <a href="<?php echo $myspaceCode; ?>" target="_blank"></a></p>
    </div>
    <!– artist details close –>

    <!– ARTIST CLOSE –>

    # September 24, 2009 at 6:51 pm

    *Moved to CMS section since it’s dealing with Custom Fields*

    I don’t think I can answer this without a little more information. What is it outputting that "isn’t working"? If you have a link to the page that’s doing it, that’d help too.

    Also, you might want to consider changing the code slightly. What if the artist doesn’t have a Myspace page? I would do something like this:

    Code:
    if ($artistimageCode) {
    echo ‘music box for life artist‘;
    }
    if ($websiteCode) {
    echo ‘

    Website:

    ‘;
    }
    if ($myspaceCode) {
    echo ‘

    Myspace:

    ‘;
    }

    This way, if they don’t have a website, it won’t show "Website: ", it’ll just bump the Myspace up one line.

    # September 24, 2009 at 11:24 pm

    Hey DOC

    I didnt know where the post went – so I reposted after searching for it!

    Thank for the suggestion – this is more of testing the water ie custom fields. Every artist involved the project with have a website and a myspace page. The problem is when I try to ADD a custom field in the backend of WP it says "An unidentified error has occured".

    So let’s say this is what I wanted:

    Code:

    < ?php
    $artistimageCode = get_post_meta ($post ->ID, ‘artist-image’, true);
    $websiteCode = get_post_meta ($post ->ID, ‘website-address’, true);
    $myspaceCode = get_post_meta ($post ->ID, ‘myspace’, true);
    ?>

    # September 24, 2009 at 11:47 pm

    I’ve never heard of WP doing that, might be a problem with your install. You should technically be allowed to have unlimited custom fields. If you try deleting your first custom field and adding another, what happens?

    # September 25, 2009 at 6:48 am

    hey DOC

    This really stumped me too – as my scripting looked to be pretty much ok. But I’ve fixed it – it must have been some weird little bug in my system.

    I updated my version of WP to the latest (just in case 2.83 > 2.84) and deleted all my previous entries for custom fields. I re-inputted them and it’s working! I have no idea what the exact cause was – but it was worth the try :-)

    Thanks for that!

    Have a great weekend!

    # September 26, 2009 at 12:31 am

    Actually Doc,

    I’ve coem back with my tail between my legs! I’m trying to do what you suggested ie with the "if" statement as some of the artists only have myspace accounts (and not band websites) – this is what i have and what is working for me at the moment (without the IF statement)

    Code:
    < ?php
    $artistimageCode = get_post_meta ($post ->ID, ‘artist-image’, true);
    $websiteCode = get_post_meta ($post ->ID, ‘website-address’, true);
    $myspaceCode = get_post_meta ($post ->ID, ‘myspace’, true);
    ?>

    more info

    ” target=”_blank”>Visit Website
    ” target=”_blank”>Visit Myspace

    My attempt at implementing your code:

    Code:
    < ?php
    $artistimageCode = get_post_meta ($post ->ID, ‘artist-image’, true);
    $websiteCode = get_post_meta ($post ->ID, ‘website-address’, true);
    $myspaceCode = get_post_meta ($post ->ID, ‘myspace’, true);
    ?>

    more info

    < ?php
    if ($websiteCode) {
    echo '

    Website: ‘;
    }
    if ($myspaceCode) {
    echo ‘

    Myspace:

    ‘;
    }
    ?>

    Any ideas? Thanks :-/

    # September 26, 2009 at 11:57 am

    I’m not all that good at php, but what you have done with Doc’s code looks ok to me!

    I’m assuming the problem is that the links aren’t showing up at all?

    I’m guessing you have some css styling on the anchor tags to give them height and width?
    ie., you have this:

    Code:
    echo ‘

    Website:

    ‘;

    rather than:

    Code:
    echo ‘

    Website: Visit Website

    ‘;

    Other than that, I sometimes find that my supposedly empty custom fields (when made using the flutter plugin) have blank characters in them.
    So I use the following:

    Code:
    < ?php
    $stripped_websiteCode = strip_tags($websiteCode);
    if ($stripped_websiteCode != '' && $stripped_websiteCode != '&nbsp') {
    echo '

    Website: ‘;
    }
    ?>

    I hope I haven’t led you up the garden path here. I’m pretty new to php.

    # September 26, 2009 at 12:12 pm

    Actually… ignore all that php stuff at the bottom. I’m almost certain the issue I hinted at is exclusively related to custom fields created with the flutter plugin.
    Sorry :roll:

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".