WordPress.org

Ready to get started?Download WordPress

Forums

Custom Field and Blog Page (7 posts)

  1. imlost
    Member
    Posted 3 years ago #

    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!

  2. SimonJ
    Member
    Posted 3 years ago #

    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.

  3. imlost
    Member
    Posted 3 years ago #

    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("http://myweb.com/wp-content/uploads/2011/01/<?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("http://myweb.com/wp-content/uploads/2011/01/Banner-Runner.jpg") 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("http://myweb.com/wp-content/uploads/2011/01/") 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

  4. SimonJ
    Member
    Posted 3 years ago #

    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("http://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.

  5. imlost
    Member
    Posted 3 years ago #

    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("http://mysite.com/wp-content/uploads/2011/01/23Reverse-00.jpg") top center no-repeat;
    }
    
    #header-old{
    		background: #fff url("http://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?

  6. imlost
    Member
    Posted 3 years ago #

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

    Simon thanks for all your help!

  7. SimonJ
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic