ThemeShaper Forums » Thematic

Problems setting custom field in functions.php

(8 posts)
  • Started 9 years ago by Jonathan D. Baker
  • Latest reply from Chris
  • This topic is resolved
  1. I'm trying to set up a fat footer using a theme action in my child theme functions.php file but I keep getting the error: 'Warning: Invalid argument supplied for foreach() in...' I've used the #block-3 custom field snippet below in other page templates just fine, and can't seem to figure out what's going on here. I'm using the Custom Fields Template plugin on WP 2.8.3.

    <?php
    function amalgamis_fat_footer() { ?>
    
        <div id="block-3" class="content-box">
            <h2>Block #3</h2>
            <div class="entry">
    <?php
    $block = get_post_meta($post->ID, 'Block #3');
    foreach(($block) as $blocks) {
        echo $blocks;
    }
    ?>
            </div><!-- /.entry -->
        </div><!-- /#block-3 -->
    
    <?php }
    add_action('thematic_abovefooter', 'amalgamis_fat_footer');
    ?>
    Posted 9 years ago #
  2. Hi,

    without testing your code .. if you use a global variable you should start your function with:

    global $post;

    Chris

    Posted 9 years ago #
  3. I appreciate your timely response!

    I'm confused as to why I'd want to set a global variable. I'm reusing the same snippet (while changing $post->ID, obv.) at various parts on a single page. Are you saying that nesting functions inside of the amalgamis_fat_footer() function would be a better avenue?

    Edit: So I decided it'd be much more resourceful to wrap everything up in a function and then just pass in a parameter to be set as the relevant ID. Unfortunately, I'm getting the "Warning: Invalid argument supplied for foreach() in /home/..." again...

    <div class="section clear" id="autobiography">
        <div id="autobiography" class="content-box">
            <h2>Autobiography (of sorts)</h2>
            <div class="entry">
    <?php
    function amalgamis_cfvalue($id) {
        $cfvalue = get_post_meta($post->ID, $id, true);
        foreach(($cfvalue) as $value) {
            return $value;
        }
    }
    amalgamis_cfvalue('autobiography');
    ?>
            </div>
        </div>
    </div>
    Posted 9 years ago #
  4. The first attempt fails because the thing that's returned by get_post_meta() is not an array .. and the second attempt fails because setting $single = TRUE will only return the first value but not an array.

    Chris

    Posted 9 years ago #
  5. Thanks for all of your help Chris - this is the first PHP script I've ever tried to write. I've broken things down just a little more, and here is where I am now:

    function stored in functions.php:

    <?php
    function amalgamis_cfvalue($cfid) {
        $cfvalue = get_post_meta($post->ID, $cfid);
        foreach(($cfvalue) as $value) {
            return $value;
        }
    }
    ?>

    call the function from about.php:

    <div class="entry">
          <?php amalgamis_cfvalue('autobiography'); ?>
     </div>

    I'm still getting the same 'Warning: Invalid argument supplied for foreach() in /home../functions.php' error returned though.

    Posted 9 years ago #
  6. :) .. stop changing the code again and again ..

    Could you please explain what you're trying to do?!

    At the moment I can tell you that again nothing or a non-array thing was returned by get_post_meta() .. you could wrap the for each() into something like: If $cfvalue is not empty and an array then do the for each() stuff. This will prevent the error message but I don't think that this will help you.

    Chris

    Posted 9 years ago #
  7. Sorry I kept updating the code. I was just trying to write a simple function to store in functions.php that I could call from any of my page templates to invoke the custom fields plugin without having to write a foreach statement each time. The problem was with the scope of the $post variable. I was only passing a desired 'key' in as a parameter to the function stored in functions.php, and once I set it up to pass in the 'post->ID' as well, voila.

    functions.php

    <?php
    function amalgamis_cf($id, $key) {
        $cfvalue = get_post_meta($id, $key);
        if(empty($cfvalue)) {
            echo 'error.';
        } else {
            foreach ($cfvalue as $value) {
                echo $value;
            }
        }
    }
    ?>

    Call from any page template:

    <?php amalgamis_cf($post->ID,'name_of_custom_field_key'); ?>
    Posted 9 years ago #
  8. Ok .. now I got it too :)

    Posted 9 years ago #

RSS feed for this topic

Reply

You must log in to post.