• Resolved nadine00

    (@nadine00)


    Hi,

    I’m adding some meta boxes to some custom content. One of them I would like to be a simple yes/no checkbox to make a post be featured on the front. I’ve been following this tutorial: http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/

    Ii’ve done a lot of searching for checkboxes, but all the support and tutorials i’ve found is for multiple checkboxes. I only need one.

    So my question is how to turn this:

    function front_event(){
      global $post;
      $custom = get_post_custom($post->ID);
      $front_event = $custom["front_event"][0];
      ?>
      <label>Place Event On Front Page? (type yes):</label>
      <input name="front_event" value="<?php echo $front_event; ?>" />
      <?php
    }

    into proper code for a single yes/no checkbox.

    Thanks.

    Nadine

Viewing 10 replies - 1 through 10 (of 10 total)
  • Untested:

    function front_event(){
      global $post;
      $custom = get_post_custom($post->ID);
      $front_event = $custom["front_event"][0];
      if( $front_event ){ $checked = "checked=\"checked\""; }else{ $checked = "";}
      ?>
      <label>Place Event On Front Page? (type yes):</label>
      <input type="checkbox" name="front_event" value="true" <?php echo $checked; ?> />
      <?php
    }

    HTH

    David

    Thread Starter nadine00

    (@nadine00)

    Hey Thanks so much! That worked great. 🙂

    Additional question. I’m trying to pull just one post via that check box. So I made up some arguments for a query:

    <?php $event =array(
    		'post_type' => 'Events',
    		'posts_per_page' => 1,
    		'meta_key' => 'front_event',
    		'meta_value' => true
    
    		);?>
    
    <?php query_posts($event);?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    ....etc...

    This usually works with just straight up custom fields in categories, but not here. What am I doing wrong? Do I need to access this information from the check box in a different manner?

    Thanks again. Sorry for the questions. this is my first time using custom post types.

    Nadine

    Thread Starter nadine00

    (@nadine00)

    I could do something like this:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    But for some reason that comes back as nothing. Suggestions?

    Nadine

    Thread Starter nadine00

    (@nadine00)

    And of course the ghetto version doesn’t work, because its in the loop. So…anyone? Suggestions?

    Check if the meta is saving true or 1, and is it a syntax error Events or events, slug lowercase or name first caps?

    It should be working looking at your code, but php is case sensitive.

    UNTESTED

    <?php
    $temp_query = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
        $args=array(
    		'post_type' => 'events',
    		'posts_per_page' => 1,
    		'order' =>'DESC',
    		'meta_query' => array( array( 'key' => 'front_event', 'value' => true ))
    	);
        $wp_query->query( $args );
    ?>
    <?php if ( $wp_query->have_posts() ) : ?>
        <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
    	<?php endwhile; ?>
    <?php endif; ?>
    <?php $wp_query = $temp_query; ?>

    David

    Thread Starter nadine00

    (@nadine00)

    the meta echos out as “true” from inside the loop.

    the in loop one doesn’t work because in my ghetto attempt I forgot that it has to be in the query array part. I want it to display posts in custom post type AND have front_event checked.

    The case is right. Maybe i have to do some kind of global query for that piece of data in front_event and then use it in a post query?

    Thread Starter nadine00

    (@nadine00)

    Ha!

    'value' => true

    changed to

    'value' => 'true'

    That really needs to be somewhere in the codex, also SO glad it wasn’t anything else.

    It should work on any page, as you are looking for the first post type = event that has the meta value set, then getting this post returned in a query.

    I cannot see an issue with this, it should be ‘outside the loop’?

    This is how I use it for a ‘page of posts’, and here is a post on using it, it might help.

    David

    That really needs to be somewhere in the codex, also SO glad it wasn’t anything else.

    I tend to load $variables and use these in the args!

    Glad it is sorted, wanna mark this one resolved?

    David

    Thread Starter nadine00

    (@nadine00)

    Yep changed! Thanks for the responses. Appreciated. 😀

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘add_meta_box …as a checkbox’ is closed to new replies.