WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Problem using custom fields within WordPress functions

[Resolved] Problem using custom fields within WordPress functions

  • 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!

Viewing 14 replies - 1 through 14 (of 14 total)
  • It may be that $post->ID is not available. Did you declare $post as a global?

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

    Just add global $post; after your function declaration.

    function blah_blah() {
       global $post;
       // rest of function
    }

    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!

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

    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″

    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.

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

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

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

    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?

    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.

    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) );

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

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] Problem using custom fields within WordPress functions’ is closed to new replies.
Skip to toolbar