Last active
June 8, 2024 09:14
-
-
Save simbasounds/44862000d5eacf9aac6cee41e6182ee1 to your computer and use it in GitHub Desktop.
Get all taxonomy terms of the current post in the loop and output them as a list of checked and unchecked items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
The shortcode will detect the current post type. | |
Usage: specify your taxonomy like so (defaults to "category"): | |
[checked-list taxonomy="portfolio"] | |
Add to functions.php | |
--> | |
<?php // Consider excluding opening PHP tag | |
function display_checked_list_shortcode( $atts ) { | |
global $post; | |
// Attributes | |
$atts = shortcode_atts( | |
array( | |
'taxonomy' => 'category', | |
), | |
$atts, | |
'checked-list' | |
); | |
$taxonomy = $atts['taxonomy']; | |
// Get the current post type | |
$post_type = get_post_type( $post ); | |
if ( ! $post_type ) { | |
return ''; | |
} | |
// Get the terms | |
$terms = get_terms(array( | |
'taxonomy' => $taxonomy, | |
'hide_empty' => false, | |
)); | |
if (is_wp_error($terms) || empty($terms)) { | |
return ''; | |
} | |
// Get the checked terms for the current post | |
$checked_terms = wp_get_post_terms($post->ID, $taxonomy, array('fields' => 'ids')); | |
$output = '<ul class="checkbox-list">'; | |
foreach ($terms as $term) { | |
$checked_class = in_array($term->term_id, $checked_terms) ? 'checked' : ''; | |
$output .= sprintf('<li class="%s">%s</li>', esc_attr($checked_class), esc_html($term->name)); | |
} | |
$output .= '</ul>'; | |
return $output; | |
} | |
add_shortcode( 'checked-list', 'display_checked_list_shortcode' ); | |
// Consider excluding closing PHP tag | |
?> | |
<!-- Add to stylesheet --> | |
<style type="text/css"> | |
ul.checkbox-list { | |
margin-top: 0; | |
padding-left: 0; | |
list-style-type: none; | |
} | |
ul.checkbox-list li { | |
padding-bottom: 15px; | |
} | |
ul.checkbox-list li.checked { | |
color: #0C0B0B; | |
font-weight: 500; | |
} | |
ul.checkbox-list li:before { | |
width: 12px; | |
display: inline-block; | |
margin-right: 10px; | |
} | |
ul.checkbox-list li.checked:before { | |
content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 17 12' style='enable-background:new 0 0 17 12;' xml:space='preserve'%3E%3Cstyle type='text/css'%3E .st0{fill:none;stroke:%23E19530;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;} %3C/style%3E%3Cpath class='st0' d='M1,6l5,5L16,1'/%3E%3C/svg%3E"); | |
width: 15px; | |
margin-right: 7px; | |
} | |
ul.checkbox-list li:not(.checked):before { | |
content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 14 14' style='enable-background:new 0 0 14 14;' xml:space='preserve'%3E%3Cstyle type='text/css'%3E .st0{fill:none;stroke:%237C7878;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;} %3C/style%3E%3Cpath class='st0' d='M1,13L13,1'/%3E%3Cpath class='st0' d='M13,13L1,1'/%3E%3C/svg%3E"); | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment