Support » Fixing WordPress » Custom Field values do not display

  • Resolved bconstant

    (@bconstant)


    I have been struggling for hours to get Custom Fields to show up in my posts. This is ridiculous: the documentation says these are supposed to display by default but they don’t, and the Function Reference doesn’t give any useful advice on how to write a function to enable this in the_content();/the loop/whatever. In fact, I can’t believe how much of the documentation is useless to the general user, since so little of it has been updated to advise on use of functions in hooks/filters (new in 2.8? 3?), and it’s nearly impossible to tell when it’s either necessary or redundant because you can’t immediately see what the actual function looks like anymore, it’s all buried out of view! Arghhh! </rant>

    Thanks 1,000 times over if you are able to help. I <3 WordPress but am feeling quite alienated by the new direction the latest versions are taking. I understand the need to separate custom code from functional core, but the separation has added a layer of abstraction that is terribly inaccessible to the motivated learner with little hard code experience.

    That out of the way, can anyone offer advice please (besides to give up)? For lack of any better ideas, I decided to simply experiment by inserting this code into single.php, below the_post():

    $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        echo $key . " => " . $value . "<br />";
      }
    $mykey_values = get_post_custom_values('my_key');
      foreach ($mykey_values as $key => $value) {
        echo "$key  => $value ('my_key')<br />";
      }

    It spits out the following on a single post page:

    2 => at (my custom key)
    Warning: Invalid argument supplied for foreach() in /home/yada/yada/url/single.php on line 26

    Uhhh… the two echo statements are coded differently (they are copied straight from examples in the function reference) but changing the 2nd to match first doesn’t seem to matter, since the error is thrown in the line before.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Did you add this the_meta() to your posts?

    Hi FishDogFish,

    Thanks for the tip. Adding this to single.php is fine, but it gives me no control over positioning within the flow of content. I would like to introduce this somewhere into the_content() by adding code to functions.php, but the Codex entries for the_meta() and the_content() don’t show how to do this, and none of the Codex or forum entries I’ve looked at give a clear enough example.

    [What codes samples I find are confusing since there is a disconnect between the older method of creating customized php files in child themes and the new model of constructing customizations in functions.php using code hooks, á là Thematic. I constantly feel on the verge of making a breakthrough in understanding only to be thwarted by simple problems like this, thus my frustration.]

    Am I failing to understand the distinction between modifying files in my child theme and writing code in functions.php? Or if I have the right idea, can you help me learn how to write a custom function to inject the_meta() into my single post loop?

    The custom field data is stored in an unordered list called post-meta

    You can position/style it in the style.css like this…

    .post-meta{margin:10px;padding:5px;}

    .post-meta li{margin-bottom:5px;}

    .post-meta-key{color:green;}

    I think we were misunderstanding one another—I definitely misunderstand PHP on a regular basis. Using the_meta() as you suggested was what I needed to do.

    This is what I wrote in functions.php:

    // Remove Default Single Post Loop
    function remove_singlepost() {
    	remove_action('thematic_singlepost', 'thematic_single_post');
    	}
    add_action('init', 'remove_singlepost');
    
    // Add my Single Post Loop
    function my_single_post() { ?>
    			<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
        			<?php //thematic_postheader(); ?>
    				<?php the_date('', '<hr />', '<br />'); ?>
    				<?php $cat1 = get_post_ancestors($post);
    					$cat2 = the_category();
    					cat_is_ancestor_of( $cat1, $cat2 ); ?>
    				<?php //the_category(); ?><!--<br />-->
    				<h2 class="entry-title"><a href="<?php the_permalink(); ?>" name="<?php echo get_the_date('ymd'); ?>" id="<?php echo get_the_date('ymd'); ?>"><?php the_title(); ?></a></h2>
    				<?php $key="mykey"; echo get_post_meta($post->ID, $key, true); ?>
    				<?php the_meta(); ?>
    				<div class="entry-content">
    				<?php thematic_content(); ?>
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    				</div>
    				<?php //thematic_postfooter(); ?>
    			</div><!-- .post -->
    		<?php
    }
    add_action('thematic_singlepost', 'my_single_post');

    Thanks again for your help, FishDogFish. I think a night’s sleep and strong coffee did the trick.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Field values do not display’ is closed to new replies.