✨refactor: Improve resource filter templates and styles; enhance layout and dynamic taxonomy handling
This commit is contained in:
109
assets/script.js
109
assets/script.js
@@ -8,91 +8,41 @@ jQuery(document).ready(function ($) {
|
||||
*/
|
||||
function triggerFiltering(paged = 1) {
|
||||
let searchTerm = $('#search').val();
|
||||
let appliedFilters = [];
|
||||
|
||||
let appliedFilters = [];
|
||||
let typeFilters = [];
|
||||
let subjectFilters = [];
|
||||
// Collect selected taxonomy filters dynamically
|
||||
let taxonomyFilters = {};
|
||||
$('input[type="checkbox"]:checked').each(function () {
|
||||
let taxonomy = $(this).attr('name').replace('[]', ''); // Extract taxonomy name
|
||||
|
||||
let selectedTypes = $('input[name="resource_type[]"]:checked')
|
||||
.map(function () {
|
||||
return $(this).closest('label').text().trim();
|
||||
})
|
||||
.get();
|
||||
if (!taxonomyFilters[taxonomy]) {
|
||||
taxonomyFilters[taxonomy] = [];
|
||||
}
|
||||
|
||||
let selectedSubjects = $('input[name="resource_subject[]"]:checked')
|
||||
.map(function () {
|
||||
return $(this).closest('label').text().trim();
|
||||
})
|
||||
.get();
|
||||
taxonomyFilters[taxonomy].push($(this).val());
|
||||
});
|
||||
|
||||
// Search Term
|
||||
if (searchTerm) {
|
||||
appliedFilters.push(
|
||||
`<span class="filter-item" data-type="search" data-value="${searchTerm}">
|
||||
<strong>Search:</strong> "${searchTerm}"
|
||||
<button class="remove-filter" aria-label="Remove Search">×</button>
|
||||
</span>`
|
||||
);
|
||||
// Build applied filters for display
|
||||
for (let taxonomy in taxonomyFilters) {
|
||||
taxonomyFilters[taxonomy].forEach(function (term) {
|
||||
appliedFilters.push(
|
||||
`<span class="filter-item" data-type="${taxonomy}" data-value="${term}">
|
||||
<strong>${taxonomy}:</strong> ${term}
|
||||
<button class="remove-filter" aria-label="Remove ${term}">×</button>
|
||||
</span>`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Resource Types
|
||||
$('input[name="resource_type[]"]:checked').each(function () {
|
||||
const slug = $(this).val(); // Get the slug
|
||||
const name = $(this).closest('label').text().trim(); // Get the name
|
||||
|
||||
appliedFilters.push(
|
||||
`<span class="filter-item" data-type="resource_type" data-value="${slug}">
|
||||
<strong>Type:</strong> ${name}
|
||||
<button class="remove-filter" aria-label="Remove ${name}">×</button>
|
||||
</span>`
|
||||
);
|
||||
|
||||
typeFilters.push(
|
||||
`${name}`
|
||||
);
|
||||
});
|
||||
|
||||
// Resource Subjects
|
||||
selectedSubjects.forEach(function (subject) {
|
||||
appliedFilters.push(
|
||||
`<span class="filter-item" data-type="resource_subject" data-value="${subject}">
|
||||
<strong>Subject:</strong> ${subject}
|
||||
<button class="remove-filter" aria-label="Remove ${subject}">×</button>
|
||||
</span>`
|
||||
);
|
||||
|
||||
subjectFilters.push(
|
||||
`${subject}`
|
||||
);
|
||||
});
|
||||
|
||||
$('#applied-filters').html(
|
||||
appliedFilters.length ? appliedFilters.join(' ') : 'None'
|
||||
);
|
||||
|
||||
$('#type_text').html(
|
||||
typeFilters.length ? typeFilters.join(', ') : 'Resource Type'
|
||||
);
|
||||
$('#subject_text').html(
|
||||
subjectFilters.length ? subjectFilters.join(', ') : 'Subject Tags'
|
||||
);
|
||||
$('#applied-filters').html(appliedFilters.length ? appliedFilters.join(' ') : 'None');
|
||||
|
||||
let formData = {
|
||||
action: 'filter_resources',
|
||||
nonce: resourceFilterAjax.nonce,
|
||||
search: searchTerm,
|
||||
paged: paged,
|
||||
sort_order: $('#sort-order').val(),
|
||||
resource_type: $('input[name="resource_type[]"]:checked')
|
||||
.map(function () {
|
||||
return this.value;
|
||||
})
|
||||
.get(),
|
||||
resource_subject: $('input[name="resource_subject[]"]:checked')
|
||||
.map(function () {
|
||||
return this.value;
|
||||
})
|
||||
.get(),
|
||||
sort_order: $('#sortOrder').val(),
|
||||
...taxonomyFilters, // Include taxonomy filters dynamically
|
||||
};
|
||||
|
||||
// Perform AJAX request
|
||||
@@ -112,7 +62,7 @@ jQuery(document).ready(function ($) {
|
||||
}
|
||||
|
||||
// Handle sort order change
|
||||
$('#sort-order').on('change', function () {
|
||||
$('#sortOrder').on('change', function () {
|
||||
triggerFiltering();
|
||||
});
|
||||
|
||||
@@ -148,18 +98,13 @@ jQuery(document).ready(function ($) {
|
||||
// Remove the corresponding filter
|
||||
if (filterType === 'search') {
|
||||
$('#search').val('');
|
||||
} else if (filterType === 'resource_type') {
|
||||
$('input[name="resource_type[]"]:checked').each(function () {
|
||||
} else {
|
||||
// Dynamically handle taxonomy filters
|
||||
$(`input[name="${filterType}[]"]:checked`).each(function () {
|
||||
if ($(this).val() === filterValue) { // Match the slug, not the name
|
||||
$(this).prop('checked', false);
|
||||
}
|
||||
});
|
||||
} else if (filterType === 'resource_subject') {
|
||||
$('input[name="resource_subject[]"]:checked').each(function () {
|
||||
if ($(this).closest('label').text().trim() === filterValue) {
|
||||
$(this).prop('checked', false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Re-trigger filtering after removing the filter
|
||||
|
||||
@@ -67,13 +67,13 @@ class ContentFilterPlugin {
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
check_admin_referer('content_filter_settings');
|
||||
|
||||
$post_types = isset($_POST['post_types']) ? array_map('sanitize_text_field', $_POST['post_types']) : [];
|
||||
$postTypes = isset($_POST['post_types']) ? array_map('sanitize_text_field', $_POST['post_types']) : [];
|
||||
$taxonomies = isset($_POST['taxonomies']) ? array_map('sanitize_text_field', $_POST['taxonomies']) : [];
|
||||
$posts_per_page = isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 12;
|
||||
$homepage_taxonomy = isset($_POST['homepage_taxonomy']) ? sanitize_text_field($_POST['homepage_taxonomy']) : '';
|
||||
|
||||
update_option('content_filter_homepage_taxonomy', $homepage_taxonomy);
|
||||
update_option('content_filter_post_types', $post_types);
|
||||
update_option('content_filter_post_types', $postTypes);
|
||||
update_option('content_filter_taxonomies', $taxonomies);
|
||||
update_option('content_filter_posts_per_page', $posts_per_page);
|
||||
|
||||
@@ -81,7 +81,7 @@ class ContentFilterPlugin {
|
||||
}
|
||||
|
||||
// Get saved options
|
||||
$post_types = get_option('content_filter_post_types', []);
|
||||
$postTypes = get_option('content_filter_post_types', []);
|
||||
$taxonomies = get_option('content_filter_taxonomies', []);
|
||||
$posts_per_page = get_option('content_filter_posts_per_page', 12);
|
||||
$homepage_taxonomy = get_option('content_filter_homepage_taxonomy', '');
|
||||
@@ -99,7 +99,7 @@ class ContentFilterPlugin {
|
||||
<p>Select the post types to include in the filter.</p>
|
||||
<?php foreach ($all_post_types as $post_type): ?>
|
||||
<label>
|
||||
<input type="checkbox" name="post_types[]" value="<?php echo esc_attr($post_type->name); ?>" <?php checked(in_array($post_type->name, $post_types)); ?>>
|
||||
<input type="checkbox" name="post_types[]" value="<?php echo esc_attr($post_type->name); ?>" <?php checked(in_array($post_type->name, $postTypes)); ?>>
|
||||
<?php echo esc_html($post_type->labels->singular_name); ?>
|
||||
</label><br>
|
||||
<?php endforeach; ?>
|
||||
@@ -129,7 +129,7 @@ class ContentFilterPlugin {
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<p><input type="submit" class="button-primary" value="Save Settings"></p>
|
||||
<p style="margin-top: 1.5rem;"><input type="submit" class="button-primary" value="Save Settings"></p>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
@@ -216,6 +216,7 @@ class ContentFilterPlugin {
|
||||
private function getTemplate($type) {
|
||||
if ($type === 'homepage') {
|
||||
$homepage_taxonomy = get_option('content_filter_homepage_taxonomy', '');
|
||||
|
||||
if (!empty($homepage_taxonomy) && taxonomy_exists($homepage_taxonomy)) {
|
||||
$GLOBALS['homepage_taxonomy'] = $homepage_taxonomy;
|
||||
return 'filter-homepage.php';
|
||||
@@ -290,55 +291,32 @@ class ContentFilterPlugin {
|
||||
* @return WP_Query The query object
|
||||
*/
|
||||
private function getQuery() {
|
||||
$postTypes = get_option('content_filter_post_types', ['post']); // Get selected post types from admin
|
||||
$postCount = get_option('content_filter_posts_per_page', 12); // Get posts per page from admin
|
||||
$strSearch = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc';
|
||||
|
||||
$query_args = [
|
||||
'post_type' => get_option('content_filter_post_types', ['post']),
|
||||
'posts_per_page' => get_option('content_filter_posts_per_page', 12),
|
||||
'post_type' => $postTypes,
|
||||
'posts_per_page' => $postCount,
|
||||
'paged' => max(1, get_query_var('paged', 1)), // Get current page number
|
||||
'tax_query' => $this->buildDynamicTaxQuery(),
|
||||
's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
's' => $strSearch,
|
||||
];
|
||||
|
||||
// Sorting logic
|
||||
$query_args = $this->applySorting($query_args, $sort_order);
|
||||
|
||||
$tax_query = [];
|
||||
|
||||
if (!empty($_POST['resource_type'])) {
|
||||
$resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']);
|
||||
|
||||
$query_args['tax_query'][] = [
|
||||
'taxonomy' => 'resource_type',
|
||||
'field' => 'slug',
|
||||
'terms' => $resType,
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($_POST['resource_subject'])) {
|
||||
$query_args['tax_query'][] = [
|
||||
'taxonomy' => 'resource_subject',
|
||||
'field' => 'slug',
|
||||
'terms' => array_map('sanitize_text_field', $_POST['resource_subject']),
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = [
|
||||
'relation' => 'AND', // Both filters must match
|
||||
...$tax_query
|
||||
];
|
||||
}
|
||||
|
||||
return new WP_Query($query_args);
|
||||
} else {
|
||||
return new WP_Query([
|
||||
'post_type' => get_option('content_filter_post_types', ['post']),
|
||||
'posts_per_page' => get_option('content_filter_posts_per_page', 12),
|
||||
'post_type' => $postTypes,
|
||||
'posts_per_page' => $postCount,
|
||||
'paged' => max(1, get_query_var('paged', 1)), // Get current page number
|
||||
'tax_query' => $this->buildDynamicTaxQuery(),
|
||||
's' => $strSearch,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -365,27 +343,27 @@ class ContentFilterPlugin {
|
||||
switch ($sort_order) {
|
||||
case 'date_asc':
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'ASC';
|
||||
$query_args['order'] = 'ASC';
|
||||
break;
|
||||
|
||||
case 'date_desc':
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'DESC';
|
||||
$query_args['order'] = 'DESC';
|
||||
break;
|
||||
|
||||
case 'title_asc':
|
||||
$query_args['orderby'] = 'title';
|
||||
$query_args['order'] = 'ASC';
|
||||
$query_args['order'] = 'ASC';
|
||||
break;
|
||||
|
||||
case 'title_desc':
|
||||
$query_args['orderby'] = 'title';
|
||||
$query_args['order'] = 'DESC';
|
||||
$query_args['order'] = 'DESC';
|
||||
break;
|
||||
|
||||
default:
|
||||
$query_args['orderby'] = 'date';
|
||||
$query_args['order'] = 'DESC';
|
||||
$query_args['order'] = 'DESC';
|
||||
}
|
||||
|
||||
return $query_args;
|
||||
@@ -469,20 +447,21 @@ class ContentFilterPlugin {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function buildQueryArgs() {
|
||||
$post_types = get_option('content_filter_post_types', []);
|
||||
|
||||
$postTypes = get_option('content_filter_post_types', []);
|
||||
$postCount = get_option('content_filter_posts_per_page', 12); // Get posts per page from admin
|
||||
$strSearch = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
|
||||
$sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc';
|
||||
|
||||
$query_args = [
|
||||
'post_type' => !empty($post_types) ? $post_types : ['post'],
|
||||
'posts_per_page' => get_option('content_filter_posts_per_page', 12),
|
||||
'post_type' => !empty($postTypes) ? $postTypes : ['post'],
|
||||
'posts_per_page' => $postCount,
|
||||
'paged' => isset($_POST['paged']) ? intval($_POST['paged']) : 1, // Get current page number
|
||||
'tax_query' => $this->buildDynamicTaxQuery(),
|
||||
's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
's' => $strSearch,
|
||||
];
|
||||
|
||||
$query_args = $this->applySorting($query_args, $sort_order);
|
||||
$query_args['tax_query'] = $this->buildTaxQuery();
|
||||
$query_args['tax_query'] = $this->buildDynamicTaxQuery();
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
@@ -498,7 +477,7 @@ class ContentFilterPlugin {
|
||||
* @since 1.4.0
|
||||
*/
|
||||
private function buildDynamicTaxQuery() {
|
||||
$taxonomies = get_option('content_filter_taxonomies', []);
|
||||
$taxonomies = get_option('content_filter_taxonomies', []); // Get selected taxonomies from admin
|
||||
$tax_query = [];
|
||||
|
||||
if (!empty($taxonomies)) {
|
||||
@@ -526,50 +505,6 @@ class ContentFilterPlugin {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** Builds a taxonomy query array for filtering resources by type and subject.
|
||||
*
|
||||
* Constructs a taxonomy query based on the 'resource_type' and 'resource_subject'
|
||||
* POST parameters. The function checks if these parameters are present and
|
||||
* properly sanitizes them before adding them to the query. If both parameters
|
||||
* are provided, the query will require both conditions to match using an 'AND'
|
||||
* relation.
|
||||
*
|
||||
* @return array The constructed tax query array, or an empty array if no filters are applied.
|
||||
*/
|
||||
|
||||
private function buildTaxQuery() {
|
||||
$tax_query = [];
|
||||
|
||||
if (!empty($_POST['resource_type'])) {
|
||||
$resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']);
|
||||
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'resource_type',
|
||||
'field' => 'slug',
|
||||
'terms' => $resType,
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($_POST['resource_subject'])) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'resource_subject',
|
||||
'field' => 'slug',
|
||||
'terms' => array_map('sanitize_text_field', $_POST['resource_subject']),
|
||||
'operator' => 'IN'
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($tax_query)) {
|
||||
return [
|
||||
'relation' => 'AND', // Both filters must match
|
||||
...$tax_query
|
||||
];
|
||||
}
|
||||
|
||||
return $tax_query;
|
||||
}
|
||||
|
||||
/** Sends an AJAX response with resource filtering results.
|
||||
*
|
||||
* Constructs a response array containing the number of resources found,
|
||||
@@ -583,24 +518,31 @@ class ContentFilterPlugin {
|
||||
|
||||
private function sendAjaxResponse($query) {
|
||||
$response = [
|
||||
'count' => $query->found_posts,
|
||||
'filters' => [
|
||||
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '',
|
||||
'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : ''
|
||||
'count' => $query->found_posts,
|
||||
'filters' => [
|
||||
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
],
|
||||
'html' => ob_get_clean(),
|
||||
'pagination' => paginate_links([
|
||||
'total' => $query->max_num_pages,
|
||||
'current' => isset($_POST['paged']) ? intval($_POST['paged']) : 1,
|
||||
'format' => '?paged=%#%',
|
||||
'add_args' => [], // Pass additional query arguments
|
||||
'html' => ob_get_clean(),
|
||||
'pagination' => paginate_links([
|
||||
'total' => $query->max_num_pages,
|
||||
'current' => isset($_POST['paged']) ? intval($_POST['paged']) : 1,
|
||||
'format' => '?paged=%#%',
|
||||
'add_args' => [], // Pass additional query arguments
|
||||
'prev_text' => '«',
|
||||
'next_text' => '»',
|
||||
'type' => 'array',
|
||||
'type' => 'array',
|
||||
])
|
||||
];
|
||||
|
||||
// Include dynamic taxonomy filters in the response
|
||||
$taxonomies = get_option('content_filter_taxonomies', []);
|
||||
|
||||
foreach ($taxonomies as $taxonomy) {
|
||||
if (!empty($_POST[$taxonomy])) {
|
||||
$response['filters'][$taxonomy] = array_map('sanitize_text_field', (array) $_POST[$taxonomy]);
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
wp_die();
|
||||
}
|
||||
|
||||
@@ -1,59 +1,36 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
$resource_types = get_terms(['taxonomy' => 'resource_type', 'hide_empty' => true]);
|
||||
$resource_subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]);
|
||||
$selected_taxonomies = get_option('content_filter_taxonomies', []); // Get selected taxonomies
|
||||
?>
|
||||
|
||||
<form class="px-4 sm:px-0" id="resource-filter">
|
||||
<!-- Search Field -->
|
||||
<!-- Search Field - Theme -->
|
||||
<div class="search-text">
|
||||
<input class="bg-[#F8F8F8] border-[#8B8B8B] border-2" type="text" id="search" name="search" placeholder="Search resources..."
|
||||
value="<?php echo isset($search) ? esc_attr($search) : ''; ?>">
|
||||
<button class="bg-[#3B65D4] text-white" type="submit">Search</button>
|
||||
<button type="reset" id="clear-search">×</button>
|
||||
<input class="bg-light border-secondary border-2 full-width" type="text" id="search" name="search" placeholder="Search..." value="<?php echo isset($search) ? esc_attr($search) : ''; ?>">
|
||||
<button class="bg-primary text-white" type="submit">Search</button>
|
||||
<button type="reset" class="bg-danger text-white" id="clear-search">×</button>
|
||||
</div>
|
||||
|
||||
<div class="search-tax">
|
||||
<!-- Resource Type Filters -->
|
||||
<div class="custom-dropdown">
|
||||
<button type="button" class="dropdown-toggle">Resource Type</button>
|
||||
<div class="dropdown-menu">
|
||||
<?php foreach ($resource_types as $type) : ?>
|
||||
<label>
|
||||
<input type="checkbox" name="resource_type[]" value="<?php echo esc_attr($type->slug); ?>" <?php echo
|
||||
(isset($_POST['resource_type']) && in_array($type->slug, (array) $_POST['resource_type'])) ? 'checked' :
|
||||
'';
|
||||
?>>
|
||||
<?php echo esc_html($type->name); ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
foreach ($selected_taxonomies as $taxonomy):
|
||||
$terms = get_terms(['taxonomy' => $taxonomy, 'hide_empty' => true]);
|
||||
$taxonomy_obj = get_taxonomy($taxonomy);
|
||||
|
||||
<!-- Resource Subject Filters -->
|
||||
<div class="custom-dropdown">
|
||||
<button type="button" class="dropdown-toggle">Resource Subject</button>
|
||||
<div class="dropdown-menu">
|
||||
<?php foreach ($resource_subjects as $subject) : ?>
|
||||
<label>
|
||||
<input type="checkbox" name="resource_subject[]" value="<?php echo esc_attr($subject->slug); ?>" <?php echo
|
||||
(isset($_POST['resource_subject']) && in_array($subject->slug, (array) $_POST['resource_subject'])) ?
|
||||
'checked' : ''; ?>>
|
||||
<?php echo esc_html($subject->name); ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Sort Container -->
|
||||
<div id="sort-container" class="flex items-start">
|
||||
<label class="pt-2" for="sort-order">Sort:</label>
|
||||
<select class="ml-2 bg-white border-[#CCC] border-2" id="sort-order">
|
||||
<option value="date_desc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : 'date_desc', 'date_desc'); ?>>Newest First</option>
|
||||
<option value="date_asc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : 'date_desc', 'date_asc'); ?>>Oldest First</option>
|
||||
<option value="title_asc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : 'date_desc', 'title_asc'); ?>>Title (A-Z)</option>
|
||||
<option value="title_desc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : 'date_desc', 'title_desc'); ?>>Title (Z-A)</option>
|
||||
</select>
|
||||
</div>
|
||||
if (!empty($terms) && !empty($taxonomy_obj)): ?>
|
||||
<div class="custom-dropdown">
|
||||
<button type="button" class="dropdown-toggle"><?php echo esc_html($taxonomy_obj->labels->singular_name); ?></button>
|
||||
<div class="dropdown-menu">
|
||||
<?php foreach ($terms as $term): ?>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($taxonomy); ?>[]" value="<?php echo esc_attr($term->slug); ?>" <?php echo (isset($_POST[$taxonomy]) && in_array($term->slug, (array) $_POST[$taxonomy])) ? 'checked' : ''; ?>>
|
||||
<?php echo esc_html($term->name); ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -16,37 +16,36 @@ if (!empty($_POST['search'])) {
|
||||
];
|
||||
}
|
||||
|
||||
// Handle the "Resource Type" filter
|
||||
if (!empty($_POST['resource_type'])) {
|
||||
$selected_types = is_array($_POST['resource_type']) ? $_POST['resource_type'] : [$_POST['resource_type']];
|
||||
$filters[] = [
|
||||
'type' => 'resource_type',
|
||||
'value' => esc_html(implode(',', $selected_types)),
|
||||
'label' => '<strong>Type:</strong> ' . esc_html(implode(', ', $selected_types))
|
||||
];
|
||||
}
|
||||
// Get selected taxonomies from admin settings
|
||||
$selectedTaxonomies = get_option('content_filter_taxonomies', []);
|
||||
|
||||
// Handle the "Resource Subject" filter
|
||||
if (!empty($_POST['resource_subject'])) {
|
||||
$selected_subjects = is_array($_POST['resource_subject']) ? $_POST['resource_subject'] : [$_POST['resource_subject']];
|
||||
$filters[] = [
|
||||
'type' => 'resource_subject',
|
||||
'value' => esc_html(implode(',', $selected_subjects)),
|
||||
'label' => '<strong>Subject:</strong> ' . esc_html(implode(', ', $selected_subjects))
|
||||
];
|
||||
// Handle dynamic taxonomy filters
|
||||
foreach ($selectedTaxonomies as $taxonomy) {
|
||||
if (!empty($_POST[$taxonomy])) {
|
||||
$selectedTerms = is_array($_POST[$taxonomy]) ? $_POST[$taxonomy] : [$_POST[$taxonomy]];
|
||||
$taxonomyObj = get_taxonomy($taxonomy);
|
||||
|
||||
if ($taxonomyObj) {
|
||||
$filters[] = [
|
||||
'type' => $taxonomy,
|
||||
'value' => esc_html(implode(',', $selectedTerms)),
|
||||
'label' => '<strong>' . esc_html($taxonomyObj->labels->singular_name) . ':</strong> ' . esc_html(implode(', ', $selectedTerms))
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Display filters as HTML
|
||||
$filter_html = '';
|
||||
$filterHtml = '';
|
||||
if (!empty($filters)) {
|
||||
foreach ($filters as $filter) {
|
||||
$filter_html .= '<span class="filter-item" data-type="' . esc_attr($filter['type']) . '" data-value="' . esc_attr($filter['value']) . '">'
|
||||
$filterHtml .= '<span class="filter-item" data-type="' . esc_attr($filter['type']) . '" data-value="' . esc_attr($filter['value']) . '">'
|
||||
. $filter['label']
|
||||
. ' <button class="remove-filter" aria-label="Remove ' . esc_attr($filter['type']) . '">×</button>'
|
||||
. '</span> ';
|
||||
}
|
||||
} else {
|
||||
$filter_html = 'None';
|
||||
$filterHtml = 'None';
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -57,19 +56,19 @@ if (!empty($filters)) {
|
||||
<div class="sort-filters flex items-start gap-4">
|
||||
<!-- Sort Container -->
|
||||
<div id="sort-container">
|
||||
<label for="sort-order">Sort by:</label>
|
||||
<select id="sort-order">
|
||||
<option value="date_desc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : '', 'date_desc'); ?>>Newest First</option>
|
||||
<option value="date_asc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : '', 'date_asc'); ?>>Oldest First</option>
|
||||
<option value="title_asc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : '', 'title_asc'); ?>>Title (A-Z)</option>
|
||||
<option value="title_desc" <?php selected(isset($_GET['sort_order']) ? $_GET['sort_order'] : '', 'title_desc'); ?>>Title (Z-A)</option>
|
||||
<label for="sortOrder">Sort by:</label>
|
||||
<select id="sortOrder">
|
||||
<option value="date_desc" <?php selected(isset($_GET['sortOrder']) ? $_GET['sortOrder'] : '', 'date_desc'); ?>>Newest First</option>
|
||||
<option value="date_asc" <?php selected(isset($_GET['sortOrder']) ? $_GET['sortOrder'] : '', 'date_asc'); ?>>Oldest First</option>
|
||||
<option value="title_asc" <?php selected(isset($_GET['sortOrder']) ? $_GET['sortOrder'] : '', 'title_asc'); ?>>Title (A-Z)</option>
|
||||
<option value="title_desc" <?php selected(isset($_GET['sortOrder']) ? $_GET['sortOrder'] : '', 'title_desc'); ?>>Title (Z-A)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Applied Filters -->
|
||||
<p>
|
||||
<strong>Filters applied:</strong><br>
|
||||
<span id="applied-filters"><?php echo $filter_html; ?></span>
|
||||
<span id="applied-filters"><?php echo $filterHtml; ?></span>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,34 +1,38 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) { exit; } // Prevent direct access
|
||||
|
||||
// Define dynamic post type and taxonomy
|
||||
// $postType = isset($postType) ? $postType : 'post'; // Default to 'post' if not set
|
||||
// $taxonomy = isset($taxonomy) ? $taxonomy : 'category'; // Default to 'category' if not set
|
||||
|
||||
if (!empty($resources)) :
|
||||
foreach ($resources as $resource) :
|
||||
$postID = $resource->ID;
|
||||
$postTitle = get_the_title($postID);
|
||||
$postLink = get_permalink($postID);
|
||||
$terms = get_the_terms($postID, 'resource_type');
|
||||
$postType = get_post_type($postID);
|
||||
$terms = get_the_terms($postID, $taxonomy);
|
||||
|
||||
$img = has_post_thumbnail($postID) ? get_the_post_thumbnail_url($postID, 'full') : get_field('admin', 'option')['imgDefault']['url'] ?? '';
|
||||
?>
|
||||
<div class="px-4 sm:px-0">
|
||||
<?php if (has_post_thumbnail($postID)) : ?>
|
||||
<div class="block bg-[#F3F3F3] h-[32rem]">
|
||||
<a href="<?php echo esc_url($postLink); ?>">
|
||||
<img src="<?php echo esc_url(get_the_post_thumbnail_url($postID)); ?>" alt="<?php echo esc_attr($postTitle); ?>"
|
||||
class="flex justify-center items-center w-full h-full object-contain">
|
||||
</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="block mb-4">
|
||||
<a href="<?php echo esc_url($postLink); ?>">
|
||||
<img src="<?php echo esc_url($img); ?>" alt="<?php echo esc_attr($postTitle); ?>" class="flex justify-center items-center w-full h-full object-contain">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<p class="text-sm font-thin uppercase">
|
||||
<?php echo $terms ? esc_html(strtolower($terms[0]->name)) : ''; ?>
|
||||
<p class="text-sm font-thin uppercase px-2 mb-1!">
|
||||
Post Type: <?php echo $postType; ?>
|
||||
</p>
|
||||
|
||||
<div class="flex">
|
||||
<div class="flex px-2">
|
||||
<div>
|
||||
<h3 class="text-lg"><a class="" href="<?php echo esc_url($postLink); ?>"><?php echo esc_html($postTitle); ?></a></h3>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center ml-auto pr-4">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 ml-2">
|
||||
<div class="flex ml-auto mt-1">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 ml-2">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
|
||||
</svg>
|
||||
</div>
|
||||
@@ -36,5 +40,5 @@ if (!empty($resources)) :
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<p>No resources found.</p>
|
||||
<p>No results.</p>
|
||||
<?php endif; ?>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
border: 1px solid #ccc;
|
||||
border-right: none;
|
||||
font-size: 1rem;
|
||||
padding: .5rem;
|
||||
padding: 1rem;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@
|
||||
|
||||
#resource-results {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
gap: 1.5rem;
|
||||
grid-template-columns: repeat(1, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@
|
||||
.pagination {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin-top: 20px;
|
||||
padding: 20px 0;
|
||||
}
|
||||
|
||||
.pagination ul {
|
||||
|
||||
Reference in New Issue
Block a user