WordPress.org

Ready to get started?Download WordPress

Forums

Please help me make this php more tidy (5 posts)

  1. venkmanuk
    Member
    Posted 3 years ago #

    Hi there, i'm trying to write a piece of code that removes the commas from the items generated by a post's custom fields. I'm kind of amazed how hard it is to find some ready made code to do this..

    here's a pastebin of what i would like to tidy up:
    http://wordpress.pastebin.com/QBH23g4E

    What i have works, but is very untidy and there MUST be a better way to do it. I thought i could maybe do something that repeated for each get_post_custom_keys() item.. but that didn't work/i couldn't figure it out.

    at the moment i have the following generic code repeatedly hard coded many times ( once for each possible key )

    <?php $documents = get_post_meta($post->ID, 'XXX', false);
    					if ($XXX) {
    					  	echo "<em class=\"post-meta\">XXX:</em><ul class=\"post_meta_ul\">";
    					  	foreach ($XXX as $myLink) {
    							echo "<li class=\"post-meta-key\">".$myLink."</li>";
    					  	}
    						echo "</ul>";
    					}
    				?>

    any help with this would be very much appreciated !

  2. vtxyzzy
    Member
    Posted 3 years ago #

    I can't test this, but it should be close to what you want:

    <?php
    function show_meta_info($key) {
       global $post;
       $values = get_post_meta($post->ID,$key,false);
       if ($values) {
          echo "<em class=\"post-meta\">$key:</em><ul class=\"post_meta_ul\">";
          foreach ($values as $value) {
             echo "<li class=\"post-meta-key\">".$value."</li>";
          }
          echo "</ul>";
       }
    }
    ?>
    <aside>
       <!-- related information about this post -->
       <!-- <?php the_meta(); ?> -->
       <?php show_meta_info('Documents'); ?>
       <?php show_meta_info('Email'); ?>
       <?php show_meta_info('Mobile'); ?>
       <?php show_meta_info('Social Media'); ?>
    </aside>
  3. venkmanuk
    Member
    Posted 3 years ago #

    HA ! wow.. it works perfectly and is super tidy!! ^_^

    thank you.

    so now i need to work out what you did and understand it..

    --> write a function called show_meta_info() with the parameter $key*
    // for the whole of the post?
    // variable called 'values' = an array of the values of the custom fields with the specified $key from this post.
    // one iteration of the if statement i had before but with the $key going where it needs to and the $value drops into the li
    --> run the show_meta_info() function by writing the key in there.

    * so are you using $key because it already exists in get_post_meta($post_id, $key, $single = false) ?

    .. i suppose to advance this i could try to make the code find out the names of all the keys and then write a series of show_meta_info() automatically. but for now this is perfect!

    thanks again.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    You can actually name the parameter to the function any legal variable name. I could have written this:

    function show_meta_info($foo) {

    and used $foo inside the function.

  5. venkmanuk
    Member
    Posted 3 years ago #

    declaring global $post;

    allows the function to find $post ( data? ) outwith it's own scope.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.