• I set up a multi-select field in my content type “Product”. With this, the admin user could use checkboxes to indicate which options are available for a particular product. Then, with an additional code for a cluetip, someone could mouseover the name of the options in the front-end, and have a cluetip pop up which has further information about that option (what it costs extra, what kinds of modifications are done from the original product to achieve this option, etc…) However, I want to make sure that each of these options (output filter is set to “array” right now) only displays if the administrator has checked the box. If not, the below code should just echo “nope” for every unchecked option.

    <strong> Option Array: </strong>
    <?php
    $my_arr = get_custom_field('product_options');
    
    $opts = array(
        'Option 1' => '<div>Option 1 description</div>',
        'Option 2' => '<div>Option 2 description</div>',
        'Option 3' => '<div>Option 3 description</div>',
    );
    
    foreach($my_arr as $val) {
    if (in_array($val, array_keys($opts))) echo $val . "<br/>"; else echo 'nope<br/>';
    } ?>

    Can this be done with said conditionals? Currently my efforts to have the word “nope” echoed in place of an option (in that array) which is not toggled on in the backend, has not been successful.

    How would it look if neither of the 3 options were checked. Currently, with the above code, if I have a product content type with no additional optiosn checked on by an admin, it just returns the error:

    Warning: Invalid argument supplied for foreach()

    For those who would want to suggest I ask this on a PHP forum (since it does deal with if/else statements), the current topic is going on in this Stack Overflow question:

    https://stackoverflow.com/questions/6741370/in-array-based-if-then-else-statement

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter WebmistressM

    (@webmistressm)

    Now I just gotta update my code for arrays, since CCTM nwo does em on checkbox type content fields.

    Current code I need to modify is:
    
    <strong>Available Options:</strong>
    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
    	print '<li>';
    	print $item;
    	print '</li>';
    }
    ?>
    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    em? Not sure what you mean. Have you used the output filters for this? Specifically the “formatted_list” filter:
    https://code.google.com/p/wordpress-custom-content-type-manager/wiki/formatted_list_OutputFilter

    <?php
    print_custom_field('product_options:formatted_list', array('<li>[+value+]</li>','<ul>[+content+]</ul>') );
    ?>

    You can always iterate over the array manually, but the Output Filters were designed to make printing your data as simple as possible.

    Thread Starter WebmistressM

    (@webmistressm)

    Well, I need each option in this array to have a corresponding tooltip. None of the “tooltip” plugins let you have much control, so I imagine Ill have to load the cluetip jquery library and call it. Its just Im not sure how to have the cluetip scan for certain values so it can display the correct corresponding tool tip.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Yep, you’ll have to load up the tooltip JS/CSS in your template — some of those plugins require that you set a title attribute for the link.

    You’re using a multi-select field, right? Not a checkbox field … otherwise you wouldn’t be returning an array. So you could either define a global array of tooltips (e.g. at the top of your template file) such as

    $tooltips = array(); // initialize
    $tooltips['some_value'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';

    Then use each value returned from the array to get your tooltip, e.g.

    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
        $tooltip = $tooltips[$item];
        print "<li><span title="$tooltip">$item</span></li>";
    }
    ?>

    OR you could play around with the “use distinct options/values” option when you define your multi-select field, then iterate over the array using full key/value pairs, e.g.

    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $k => $v) {
        print "<li><span title="$k">$v</span></li>";
    }
    ?>
    Thread Starter WebmistressM

    (@webmistressm)

    $tooltips = array(); // initialize
    $tooltips['some_value'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';

    https://craigsworks.com/projects/qtip2/demos/simple?source
    Here is the jquery library Im looking towards. It uses $(‘a[title]’).qtip(); so I guess the above statement would be slightly different.

    Thread Starter WebmistressM

    (@webmistressm)

    Ack. As of this morning, qtip2’s site is returning Internal Server Error. Sorry to send you to a currently-not-working link.

    (for anyone else following the thread) Just gotta wait til this page is back up and see if it is the solution.

    https://craigsworks.com/projects/qtip2/tutorials/advanced/#wordpress

    Will post info once I can try it out (once the site is accessible)

    Thread Starter WebmistressM

    (@webmistressm)

    Okay, here is what I have so far. Things still arent working but..

    In functions.php of my theme:

    if(!is_admin()){
       wp_deregister_script('jquery');
       wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
    }

    In header.php of my theme (just before wp_head):

    <script type="text/javascript"
       src="<?php bloginfo("template_url"); ?>/js/jquery.qtip.min.js"></script>

    And on single-product.php:

    <script type="text/javascript">
    $tooltips = array(); // initialize
    $tooltips['No Options'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';
    </script>
    <strong>Available Options:</strong>
    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
        $tooltip = $tooltips[$item];
    	print '<li> <span title="$tooltip">';
    	print $item;
    	print '</span> </li>';
    }
    ?>
Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘If/Else Statement for Custom Checkbox’ is closed to new replies.