1

I'm wondering if you can help me work through an issue or provide alt ideas.

I have an archive displaying a list of custom tax terms in a grid view.

My client needs to be able to turn on/off the terms from displaying in the grid. If these were posts this would be easy because I could simply have him check/uncheck a category for the post. However, taxonomies don't have that feature out of the box.

Does anyone know how I might accomplish this?

2

You might be able to accomplish this using Advanced Custom Fields: http://www.advancedcustomfields.com/resources/taxonomy/

Field groups, like the taxonomy field, can be assigned to templates. Editing the taxonomy archive page, the user would see checkboxes (or radio buttons, or a select menu) for the taxonomy terms.

In the corresponding "taxonomy archive" template (from link above)...

<?php 

$terms = get_field('taxonomy_field_name');

if( $terms ): ?>

    <ul>

    <?php foreach( $terms as $term ): ?>

        <a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a>

    <?php endforeach; ?>

    </ul>

<?php endif; ?>
  • Make sure to provide some context for links, @taruckus. See: wordpress.stackexchange.com/help/how-to-answer "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there." I know why you're recommending this, but it's not clear from the answer. – mrwweb Aug 18 '14 at 21:49
  • Thank you, I will look at ACF. Could you provide any more info about how it may help? – streetfire Aug 18 '14 at 22:21
  • Sure all; post expanded. – Taruc Aug 18 '14 at 22:53
  • Hi, just an update, ACF has helped me achieve this. You code above I think would work, but I needed something a little more specific, please see the answer I added. – streetfire Aug 26 '14 at 17:56
0

Hy,

From what I understand you want a filter to display posts that belong to a specific list of terms. As default you display all posts making all terms enabled. In order to disable a term from being displayed you need to exclude it from the loop.

<?php
// we get all the terms of the taxonomy
$terms = get_terms('mytaxonomy');

/*
excludeTerms=term1,term2,term3
as a GET parameter created by the exclude form
*/
$excludeList = array();

if (isset($_GET['excludeTerms']) && !empty($_GET['excludeTerms'])) {
    $excludeList = explode(',', $_GET['excludeTerms']);
}

/*
how to display the available terms
the permalink is get_term_link($term, $taxonomy);
*/
$termList = '';
if (!empty($terms) && !is_wp_error($terms)) {
    echo "<ul>";
    foreach ($terms as $term) {
        if (!in_array($term->name, $excludeList)) {
            echo "<li class='enabled-term'>" . $term->name . "</li>";
            if ($termList == '') {
                $termList = $term->name;
            } else {
                $termList .= ', ' . $term->name;
            }
        } else {
            echo "<li class='disabled-term'>" . $term->name . "</li>";
        }
    }
    echo "</ul>";
}

if ($termList != '') {
    $args = array(
        'post_type' => 'posttype',
        'mytaxonomy' => $termList
    );
} else {
    $args = array(
        'post_type' => 'posttype'
    );
}
query_posts($args);
while (have_posts()) {
    the_post();
    the_title();
    echo "<br />";
}
?>
    <style>
        .enabled-term, .disabled-term {
            cursor: pointer;
        }

        .disabled-term {
            color: #ddd;
        }
    </style>
    <script>
        jQuery(document).ready(function ($) {
            $('.enabled-term').live('click', function () {
                // so that one term remains enabled
                if ($('.enabled-term').length == 1)
                    return;
                $(this).removeClass('enabled-term').addClass('disabled-term');
                navigate();
            });
            $('.disabled-term').live('click', function () {
                $(this).removeClass('disabled-term').addClass('enabled-term');
                navigate();
            });
            function navigate() {
                list = $('.disabled-term');
                excludeList = '';
                for (i = 0; i < list.length; i++) {
                    if (excludeList == '')
                        excludeList = $(list[i]).text();
                    else
                        excludeList += ',' + $(list[i]).text();
                }
                window.location = window.location.origin + 
                                  window.location.pathname + 
                                  '?excludeTerms=' + excludeList;
            };
        }(jQuery));
    </script>

http://codex.wordpress.org/Function_Reference/get_terms for term list http://codex.wordpress.org/query_posts for the loop (Example 5 for tax & terms)

Hope this helps.

  • Hi, Thank you for your answer, but this is not what I was looking for. – streetfire Aug 26 '14 at 17:55
  • Take it as an idea not a solution. It may lead you somewhere. – aifrim Sep 1 '14 at 16:49
  • @streetfire you are talking about a back-end solution? – aifrim Sep 3 '14 at 14:28
  • yes a backend solution was what I needed. I was able to achieve it using Advanced Custom fields and a custom query. – streetfire Sep 3 '14 at 14:48
0

I have found a solution. Thanks to @Tarukus as his answer was helpful. Again, my goal was to display terms (not posts) from my custom taxonomy via an on/off switch. To do this, I used Advanced Custom Fields and created a "Select" field with two values (on/off). Once this field was applied to my taxonomy I could select a value for each term. The default value was set to "off". Now to display only the terms set to "on" I had to add the following code in my template file - and of course set some terms to "on".

 <?php

    $terms = get_terms('MYTAXONOMY', array());      

    if($terms)

    {

        echo '<ul>';

        foreach($terms as $term)

        {


            if( get_field('ENTERCUSTOMFIELDSLUG', 'ENTERMYTAXONOMY_'.$term->term_id) != 'ENTERFIELDVALUE' ) 
                        continue;

            echo '<li>' . $term->name . '</a></li>';

        }

        echo '</ul>';

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.