• Resolved xdesi

    (@xdesi)


    Hi,

    I created a custom meta box via the tutorial found here:
    https://codex.www.ads-software.com/Function_Reference/add_meta_box

    It works great and all the only problem is that I want it to show the current set value in the box if there is one set for this post.

    Here is the code for the box:

    function mybox(){
      echo '
    <select id="myselect" name="myselectname">
    //Need something here to check if there is a current value and show it first if there is
      <option value="no">None</option>
      <option value="top">Yes 1</option>
      <option value="footer">Yes 2/option>
    </select>';
    }

    So if I choose Yes 1 in my box it saves great and I can access this saved data from my theme.
    However I just need the box to show this value when I go to edit a post. Instead when I go to edit the post, the None value is always showing even though a different value is saved?

    So basically I need a way of retreiving the current value for the box if there is one set and if so show this option value first I guess.

    Should I set up a loop or something and use get_post_meta or is there a simple way?

Viewing 1 replies (of 1 total)
  • Thread Starter xdesi

    (@xdesi)

    OK it turned out get_post_meta did the trick.. I first had to pass $post_id into the function and then I could do this:

    function add_to_nav_box($post_id){
    //Get the Current Value
      $curr_val = get_post_meta($post_id->ID,'_addnav',true);
    
    //Layout Options Based on Current Value
      if($curr_val == "no" || $curr_val == ""){
    	  echo '<option value="no">None</option><option value="top">Yes 1</option><option value="footer">Yes 2</option>';
      }
      if($curr_val == "top"){
    	  echo '<option value="top">Yes 1</option><option value="no">None</option><option value="footer">Yes 2</option>';
      }
      if($curr_val == "footer"){
    	  echo '<option value="footer">Yes 2</option><option value="no">None</option><option value="top">Yes 1</option>';
      }
      echo'</select>';
    }

    P.S I ommited some code just to show the main parts e.g the nonce verification and the save postdata function

Viewing 1 replies (of 1 total)
  • The topic ‘How to Save Custom Meta Box Value’ is closed to new replies.