3

I am creating a taxonomy filter for WordPress archives. I have the checkboxes working so when selected they redirect the page to the correct URL based on the taxonomy slug. My problem is that after page reload the checkbox state resets to unchecked.

Here is my code:

<?php
$workoutmoves = get_terms('Exercise-Types', array('hide_empty' => 0));
?>

<?php if ($workoutmoves): ?>
<h5>Show Me</h5>
<ul class="no-bullet">
    <?php foreach($workoutmoves AS $workoutmove): ?>
    <li>
        <input class="check_box" type="checkbox" value="<?php echo $workoutmove->slug; ?>"><?php echo $workoutmove->name; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

I thought of adding a php if statement into the input field and echoing 'checked="checked"' but I don't know how to grab the correct data to identify if it should be set.

Any insights would be a huge help. Thanks.

3 Answers 3

3

You just need to get the current taxonomy term using get_queried_object() wp function and change your loop a like this:

    <?php $term_id = get_queried_object()->term_id; #getting current term_id ?>
    <?php foreach($workoutmoves AS $workoutmove): ?>
    <li>
        <input class="check_box" type="checkbox" 
        <?php $workoutmove->term_id==$term_id?'checked':''; ?>
        value="<?php echo $workoutmove->slug; ?>">
        <?php echo $workoutmove->name; ?>
    </li>
    <?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

2

I am not 100% sure what you are doing. However, I believe you want to highlight the current page a user is on. You are going to need an if() to do this. I do not know what data you have available, presumably WP will have some way of giving you the page url, something like:

if( $workoutmove->slug == $currentSlug )
    echo 'checked="checked";

If you cannot find the url from WP, it can be found in PHP as $_SERVER['REQUEST_URI'];

#

Part 2:

$bodyParts = explode( ",", $_GET['types'] );

This will give you an array like:

array( 'arms','legs','back' );

Then, I think (I am not familiar with WP), change the line in the code provided by Tom:

<?php $workoutmove->term_id==$term_id?'checked':''; ?>

To:

<?php in_array( $workoutmove->term_id, $bodyParts )  ? 'checked' : ''; ?>

Or to give you all of it:

$bodyParts = explode( ",", $_GET['types'] );
<?php $term_id = get_queried_object()->term_id; #getting current term_id ?>
<?php foreach($workoutmoves AS $workoutmove): ?>
<li>
    <input class="check_box" type="checkbox" 
    <?php in_array( $workoutmove->term_id, $bodyParts )  ? 'checked' : ''; ?>
    value="<?php echo $workoutmove->slug; ?>">
    <?php echo $workoutmove->name; ?>
</li>
<?php endforeach; ?>

5 Comments

current slug is not the best way, you need the ID, using $workoutvove->term_id = get_queried_object()->term_id. Also, echo 'checked' is enough ;)
This code worked <?php if( $workoutmove->term_id == get_queried_object()->term_id ) echo 'checked="checked"';?> However how do I display more than 1 checkbox in a selected state?
In what situation would you want to do that? You need to create an if() for each situation a checkbox need to be selected with whatever variable you have available.
So I have 5 checkboxes that change the URL - arms would go to a URL ?exercise-types=arms but I need to be able to have multiple boxes selected. The URL would look like this ?exercise-types=arms,legs,back right now this option only allows me selected taxonomies and only the first "checked"
@kslambert my friend... in that case, make a custom page for exercise-types and make a custom query using WP_Query with the parameters via GET. Is not so hard ;)
2

I'd try the following.

<?php
    $types = ( ! empty( $_GET['types'] ) ) ? explode( ',', $_GET['types'] ) : false;
    $workoutmoves = get_terms('Exercise-Types', array('hide_empty' => 0)); 
?>

<?php if ($workoutmoves): ?>
    <h5>Show Me</h5>
    <ul class="no-bullet">
<?php foreach($workoutmoves AS $workoutmove): ?>
        <li>
            <input class="check_box" type="checkbox" <?php ( $types && in_array( $workoutmove->slug, $types ) ) ? 'checked' : ''; ?> value="<?php echo $workoutmove->slug; ?>"> <?php echo $workoutmove->name; ?>
        </li>
<?php endforeach; ?>
    </ul>
<?php endif; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.