• I came up with an issue that I don’t really understand so getting around it has been tricky. I welcome any assistance.

    I am using the following code to change a Banner Logo throughout my WP installation…All is working with the exception of the page i created that holds my blog.

    ‘<?php global $wp_query;$postid = $wp_query->post->ID;echo get_post_meta($postid, ‘BannerIcon’, true); ?>’

    What is causing this issue and How would I modify this code so that if BannerIcon field is not returning a value that “newImage.png” will be the default?
    Thanks in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You should be more specific when you explain your problem…

    What exactly doesn’t work on the “page you created to holds your blog” ?

    And what is the value you normally use in this custom field? The name of an image hosted on your server? A full img html tag?

    Do you have an url when we can see it in action?

    S.

    Thread Starter imlost

    (@imlost)

    What is happening is the following…

    In the header I am using a custom field to help populate a url string that returns the page banner based on the topic of the page and what is entered into a custom field “BannerIcon”.

    ‘#header{
    background: #fff url(“https://myweb.com/wp-content/uploads/2011/01/&lt;?php global $wp_query;$postid = $wp_query->post->ID;echo get_post_meta($postid, ‘BannerIcon’, true); ?>”) top center no-repeat;
    }’

    this builds:

    ‘#header{
    background: #fff url(“https://myweb.com/wp-content/uploads/2011/01/Banner-Runner.jpg&#8221;) top center no-repeat;
    }’

    This works for all pages except “Corporate News” which is the page that holds my blog Loop. in that case it returns:

    ‘#header{
    background: #fff url(“https://myweb.com/wp-content/uploads/2011/01/&#8221;) top center no-repeat;
    }’

    The above is leaving the banner area blank because the image name is absent which is telling me that no value is returned for my custom field “BannerIcon”

    I do not know why all the other pages would work correctly and this one is not unless this has to do with the blog post loop being on the page.

    So my questions are:

    Why is this happening?
    Is there a quick snipet of code I can use that in the event “BannerIcon” has no value or returns nothing defaults to “newImage.png”? Basically an If, then, else statement.

    ~NC

    Ok…

    I guess what you call your “page that holds my blog Loop”, it is a static wordpress page you choosed as an home page in
    wp-admin => settings => reading (?)

    Anyway, I would suggest another way to get what you want…

    In the functions.php of your theme, place this function :

    function get_my_banner_icon() {
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    $meta_values = get_post_meta($thePostID, 'BannerIcon', true);
    echo $thePostID;
    if ($meta_values) {
    echo $meta_values;
    }
    else {
    echo 'newImage.png';
    }
    }

    This function get the value of the custom field “BannerIcon”. If there is such a value, it will echo it, else, it will echo “newImage.png”.

    Then in your header, call the function in your style definition to :

    #header{
    background: #fff url("https://myweb.com/wp-content/uploads/2011/01/<?php get_my_banner_icon(); ?>") top center no-repeat;
    }

    Try this and let me know if this solution is OK to get what you want to do.

    S.

    Thread Starter imlost

    (@imlost)

    With the new code I am much closer to the desired result as the “static page” I am using to shows display posts (wp-admin => settings => reading => Posts page) is returning the default value. But I am now receiving an error in the returned value on the other static pages throughout the site.

    In the returned value on the header function I am now receiving a two digit value prefixing the custom field value. below is the result of both the new and old header code as you will note “23” prefixed on the header string:

    #header{
    		background: #fff url("https://mysite.com/wp-content/uploads/2011/01/23Reverse-00.jpg") top center no-repeat;
    }
    
    #header-old{
    		background: #fff url("https://mysite.com/wp-content/uploads/2011/01/Reverse-00.jpg") top center no-repeat;
    }

    I am only 7 days into using WP so I do not know if my guess is correct but I would assume that the two digit prefix is the page id as it is different on each page?

    Thread Starter imlost

    (@imlost)

    The first echo ($postid) was causing the issue
    after commenting that out everything is working great!

    Simon thanks for all your help!

    Ah… My mistake!

    Yes indeed… I forgot to remove the first echo I used to test if I was getting the post ID!

    You should ideed remove it! Sorry!

    Glad it works. You can mark this subject as “resolved”.

    S.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Field and Blog Page’ is closed to new replies.