Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Meta Boxes select checkbox WordPress

  • SOLVED

hi,

i use in my theme posts and custom post types the custom metabox code provided from Alex [[LINK href="http://wpshout.com/create-an-in-post-theme-options-meta-box-in-wordpress/"]]here[[/LINK]] .

it includes the select array but i cant find how to call it to my template like this:

i need to make a show/hide dropdown,
if is show
//get php file
else
//do nothing

also how i can add the same way (how the Alex's code is working)
to create a checkbox

thanks a lot!

Answers (1)

2010-09-25

Duncan O'Neill answers:

Download the post-options code from the link you gave.

Alter the function hybrid_post_meta_boxes() to add the select drop-down for posts. After line 46, add this code;

'example'=>array('name'=>'Example','title'=>__('Select:'),'type'=>'select','options'=>array('opt1'=>'opt1','opt2'=>'opt2')),

so that the entire function looks like this;



function hybrid_post_meta_boxes() {

/* Array of the meta box options. */
$meta_boxes = array(
'title' => array( 'name' => 'Title', 'title' => __('Title', 'hybrid'), 'type' => 'text' ),
'description' => array( 'name' => 'Description', 'title' => __('Description', 'hybrid'), 'type' => 'textarea' ),
'image' => array( 'name' => 'Image', 'title' => __('Image:', 'hybrid'), 'type' => 'text' ),
'featured' => array( 'name' => 'Featured', 'title' => __('Featured img:', 'hybrid'), 'type' => 'text' ),
'example'=>array('name'=>'Example','title'=>__('Select:'),'type'=>'select','options'=>array('opt1'=>'opt1','opt2'=>'opt2')),

);

return apply_filters( 'hybrid_post_meta_boxes', $meta_boxes );
}


Philip comments:

thanks Duncan,

i have add the options in the array but i can't build an

if custom select is title1
include a 1.php file
if is custom select is title2
include a 2.php file
else
do nothing

to use in my templates

thanks


Duncan O'Neill comments:


$eg = get_post_meta($post->ID,"Example",true); // for the select example above

if ($eg=="title1") include(1.php);
if ($eg=="title2") include(2.php);



Philip comments:

i try to do this, its working but don't hide the content how i hide the content?


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php $eg = get_post_meta($post->ID,"showhide",true);
if ($eg=="Show")
include (TEMPLATEPATH . '/tpl/ftpr.php');
if ($eg=="Hide");
// Show/Hide Box ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><span><?php the_title(); ?></span></h2>

<div class="frev_content">
<?php the_content('Learn more...'); ?>

<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>

<div class="content_kuai">
<img src="<?php bloginfo('template_directory'); ?>/images/img_1.gif" width="122" height="19" alt="puke" />
</div><!--end of content_kuai-->

</div>
</div>


and i'm try to add different content per 'eg'


<?php $eg = get_post_meta($post->ID,"showhide",true);
if ($eg=="Show")
include (TEMPLATEPATH . '/tpl/ftpr.php');

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><span><?php the_title(); ?></span></h2>

<div class="frev_content">
<?php the_content('Learn more...'); ?>

<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>

<div class="content_kuai">
<img src="<?php bloginfo('template_directory'); ?>/images/img_1.gif" width="122" height="19" alt="puke" />
</div><!--end of content_kuai-->

</div>
</div>

if ($eg=="Hide");
// Show/Hide Box ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><span><?php the_title(); ?></span></h2>

<div class="frev_content">
<?php the_content('Learn more...'); ?>

<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php the_tags( '<p>Tags: ', ', ', '</p>'); ?>

</div>
</div>


Duncan O'Neill comments:

Hi again Philip,

I guess I'm a bit confused as to what you want to do.

It looks as if you'll need some JQuery to achieve what you're trying to do. Which I can code for you if I get some more details.

Will the content you're trying to show / hide replace the show / hide box when a user clicks the show / hide box?

thanks,

Duncan










Duncan O'Neill comments:

Philip, just trying to clarify: Is the whole goal of the exercise to show / hide content on the front-end depending on the ONE option chosen in the back-end?


Philip comments:

no, i don't need jquery,
the whole goal is to show / hide content on the front-end depending on the ONE option chosen in the back-end.

i try the code you wrote but i cant find how i add different content.

thanks!


Duncan O'Neill comments:

I'm not following you.

"Add different content"? Do you mean you want more than two options in the select drop-down in the back-end? If so, add the options to the code from the first piece of code above.

If not, check that the variables are returning properly;


$eg = get_post_meta($post->ID,"Example",true);
echo $eg;


If the variables are returning properly, it should be a matter of just following the logic you first posted;

if is show
//get php file
else
//do nothing

so;


$eg = get_post_meta($post->ID,"Example",true);
if ($eg=="title1") include(1.php);
// else do nothing


If this doesn't make sense, can you please explain "I cant find how I add different content"?

The code solution should be easy, but I'm just not understanding exactly what isn't working.

thanks,

Duncan




Duncan O'Neill comments:

Oh, you might need to do this in the front end;


$eg = get_post_meta(get_the_ID(),"Example",true);
f ($eg=="show") include(included-file.php);


instead of;

$eg = get_post_meta($post->ID,"Example",true);
if ($eg=="show") include(included-file.php);


Both of the above depend on the following line of code being part of the hybrid_post_meta_boxes function above,


'example'=>array('name'=>'Example','title'=>__('Select:'),'type'=>'select','options'=>array('show','hide')),