WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem using custom fields within WordPress functions (15 posts)

  1. hrcwebmaster
    Member
    Posted 1 year ago #

    I am creating a CMS, so I have run into some less common problems here. I am constantly having troubles using my custom fields within a WordPress function. For most of these, I have found ways around them, but there's got to be a way to have your cake and eat it too.
    Most recently I ran into this when wanting to put an RSS feed up on my pages. (No, I can't use a widget, because the problem is that I need a different feed on the sidebar of each page and want to put it in the page template and have it pull the appropriate feed link from the page's custom fields.)
    I used code from http://codex.wordpress.org/Function_Reference/fetch_feed.
    Here's the part I'm having trouble with:
    $rss = fetch_feed('http://example.com/rss/feed/goes/here');
    So in order to use my custom fields, I need to do this:
    $rss = fetch_feed('<?php $key="delicious-feed"; echo get_post_meta($post->ID,$key,true); ?>');
    Or so I thought. It won't display anything from that feed, and I know for a fact it works, because I tested it first with the actual url before putting in the code to pull the custom fields.
    This has happened with more than just RSS feed readers, but with many things, usually when the php code has to go between ''. Any tips/tricks/help is much appreciated!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    It may be that $post->ID is not available. Did you declare $post as a global?

  3. hrcwebmaster
    Member
    Posted 1 year ago #

    Sorry, I don't know about declaring $post as global, could you send me some documentation? I'm new to WordPress and PHP.
    Thanks!!

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Just add global $post; after your function declaration.

    function blah_blah() {
       global $post;
       // rest of function
    }
  5. hrcwebmaster
    Member
    Posted 1 year ago #

    Ok I tried it this way

    $rss = fetch_feed('<?php $key="delicious-feed"; echo get_post_meta($post->ID,$key,true); ?>'); {
    global  $post
    if (!is_wp_error( $rss ) ) : }
    // Checks that the object is created correctly
        // Figure out how many total items there are, but limit it to 5.
        $maxitems = $rss->get_item_quantity(5);

    and it gets error " Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in /home/..."
    and this way

    $rss = fetch_feed('<?php $key="delicious-feed"; echo get_post_meta($post->ID,$key,true); ?>') {
    global  $post
    if (!is_wp_error( $rss ) ) : }
    // Checks that the object is created correctly
        // Figure out how many total items there are, but limit it to 5.
        $maxitems = $rss->get_item_quantity(5);

    and get this error: "Parse error: syntax error, unexpected '{' in /home/hrcadmin/public_html/wp-content/themes/catch-box-child/member.php on line 126"

    Do I need to declare the global $post before I call the custom field?
    Sorry and thank you!

  6. vtxyzzy
    Member
    Posted 1 year ago #

    I think you are missing the semi-colon after $posts.

  7. hrcwebmaster
    Member
    Posted 1 year ago #

    Hmmm didn't help. Error this time:
    " Parse error: syntax error, unexpected '{' in /home/hrcadmin/public_html/wp-content/themes/catch-box-child/member.php on line 126"

  8. vtxyzzy
    Member
    Posted 1 year ago #

    I see an extra closing brace at the end of this line:

    if (!is_wp_error( $rss ) ) : }

    Remove that, and if it still doesn't work, put the entire member.php file in a pastebin and post a link to it here.

  9. hrcwebmaster
    Member
    Posted 1 year ago #

    Ok didn't work. I pasted it here: http://pastebin.com/2zjRP0ze.
    Thank you for your help!!

  10. vtxyzzy
    Member
    Posted 1 year ago #

    You created that as a private paste, so only you can view it.

  11. hrcwebmaster
    Member
    Posted 1 year ago #

    Ooops sorry I made it public. Never used paste pin before.

  12. vtxyzzy
    Member
    Posted 1 year ago #

    I get no errors when I compile the code in the pastebin, so I can't diagnose the problem.

    Do you still get the same error?

  13. hrcwebmaster
    Member
    Posted 1 year ago #

    I'm not getting an error, the page is displaying, but it's not pulling the feed it should be. The custom field being pulled, "delicious-feed" is this: http://feeds.delicious.com/v2/rss/wahousegop/alexander. So there are two stories that should be pulling the area "In the News" on the Web page and it's not.

  14. vtxyzzy
    Member
    Posted 1 year ago #

    This code does not look right to me:

    // Get a SimplePie feed object from the specified feed source.
    $rss = fetch_feed('<?php $key="delicious-feed"; echo get_post_meta($post->ID,$key,true); ?>');
    global $post;

    I believe it should be this:

    // Get a SimplePie feed object from the specified feed source.
    global $post;
    $key = 'delicious-feed';
    $rss = fetch_feed( get_post_meta($post->ID,$key,true) );
  15. hrcwebmaster
    Member
    Posted 1 year ago #

    cue >angelic voices<
    Hooray! Thank you so much!! I really appreciate it!!

Topic Closed

This topic has been closed to new replies.

About this Topic