Skip to content

Instantly share code, notes, and snippets.

@nextab
Created February 5, 2025 17:49
Show Gist options
  • Save nextab/86a98a1f4d8852187555aeb00d896306 to your computer and use it in GitHub Desktop.
Save nextab/86a98a1f4d8852187555aeb00d896306 to your computer and use it in GitHub Desktop.
Vorlage für ein benutzerfreundlicheres Such-Ergebnis für die Live-Ajax-Ergebnisse des Plugins Relevanssi
<?php
/**
* Search results template
* @package Relevanssi Live Ajax Search
*/
function get_custom_post_type_labels() {
return array(
'projects' => 'Projekte',
'post' => 'Neuigkeiten',
'attachment_image' => 'Bilder',
'attachment_other' => 'Dateien (z. B. <i class="nxt-pdf">.pdf</i>)',
'wiki' => 'Wiki',
'video' => 'Videos',
'default' => 'Weitere Ergebnisse',
'nxt_wiki_kategorien' => 'Wiki-Kategorien',
'user' => 'Kollegen'
);
}
function get_found_post_types($posts) {
$found_post_types = array();
if ($posts) {
foreach ($posts as $post) {
$post_type = get_post_type($post);
if ($post_type === 'attachment') {
$display_type = wp_attachment_is('image', $post->ID) ? 'attachment_image' : 'attachment_other';
if (!in_array($display_type, $found_post_types)) {
$found_post_types[] = $display_type;
}
} else {
if (!in_array($post_type, $found_post_types)) {
$found_post_types[] = $post_type;
}
}
}
}
return $found_post_types;
}
function render_search_index($found_post_types, $custom_post_type_labels) {
if (count($found_post_types) <= 1) return;
?>
<nav class="search-results-index" aria-label="<?php esc_attr_e('Search Results Index', 'hoenninger-intranet'); ?>">
<ul class="search-results-index__list">
<?php foreach ($found_post_types as $post_type) :
$post_type_class = sanitize_html_class('search-results-type-heading--' . $post_type);
$post_type_label = isset($custom_post_type_labels[$post_type])
? strip_tags($custom_post_type_labels[$post_type])
: get_post_type_label($post_type);
?>
<li class="search-results-index__item">
<a href="#<?php echo esc_attr($post_type_class); ?>"
class="<?php echo esc_attr($post_type_class); ?> search-results-index__link"
onclick="smoothScrollToSearchResult(event, '<?php echo esc_js($post_type_class); ?>'); return false;">
<?php echo esc_html($post_type_label); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
<?php
}
function get_post_type_label($post_type) {
$post_type_object = get_post_type_object($post_type);
return $post_type_object ? $post_type_object->labels->name : ucfirst($post_type);
}
function render_status_element($wp_query) {
$status_element = '<div class="relevanssi-live-search-result-status" role="status" aria-live="polite"><p>';
$status_element .= sprintf(
esc_html(_n('%d result found.', '%d results found.', $wp_query->found_posts, 'relevanssi-live-ajax-search')),
intval($wp_query->found_posts)
);
if ($wp_query->found_posts > 50) {
$status_element .= '<br />' . esc_html__('Showing the 50 most relevant results. Refine your search to see more.', 'hoenninger-intranet');
}
$status_element .= '</p></div>';
return $status_element;
}
function render_attachment_result($post) {
$is_image = wp_attachment_is_image($post->ID);
$file_url = wp_get_attachment_url($post->ID);
$post_date = get_the_date('d. M Y', $post);
$author = get_the_author();
$title = get_the_title();
if ($is_image) {
$image_meta = wp_get_attachment_metadata($post->ID);
$thumbnail_url = wp_get_attachment_image_url($post->ID, 'thumbnail');
?>
<div class="relevanssi-live-search-result attachment attachment_image" role="option" aria-selected="false">
<div class="attachment-image">
<a href="<?php echo esc_url($file_url); ?>"
data-pswp-width="<?php echo esc_attr($image_meta['width']); ?>"
data-pswp-height="<?php echo esc_attr($image_meta['height']); ?>"
data-pswp-alt="<?php echo esc_attr($title); ?>"
target="_blank">
<img src="<?php echo esc_url($thumbnail_url); ?>"
alt="<?php echo esc_attr($title); ?>"
loading="lazy"
width="150"
height="150"
class="gallery-image">
</a>
</div>
<div class="attachment-content">
<span class="post-meta"><?php echo esc_html("$post_date - von $author"); ?></span>
<a href="<?php echo esc_url($file_url); ?>"
data-pswp-width="<?php echo esc_attr($image_meta['width']); ?>"
data-pswp-height="<?php echo esc_attr($image_meta['height']); ?>"
data-pswp-alt="<?php echo esc_attr($title); ?>"
target="_blank">
<span class="attachment-title"><?php echo esc_html($title); ?></span>
</a>
</div>
</div>
<?php } else { ?>
<div class="relevanssi-live-search-result attachment" role="option" aria-selected="false">
<p>
<span class="post-meta"><?php echo esc_html("$post_date - von $author"); ?></span>
<a href="<?php echo esc_url($file_url); ?>" target="_blank" rel="noopener noreferrer">
<?php echo esc_html($title); ?>
</a>
</p>
</div>
<?php }
}
function render_wiki_result($post, &$displayed_terms) {
$terms = get_the_terms($post->ID, 'nxt_wiki_kategorien');
$is_child = false;
if ($terms) {
foreach ($terms as $term) {
if (!in_array($term->term_id, $displayed_terms)) {
$displayed_terms[] = $term->term_id;
?>
<div class="relevanssi-live-search-result wiki-category" role="option">
<p><a href="<?php echo esc_url(get_term_link($term)); ?>"
class="wiki-category-link"
data-module="Wiki"
data-taxonomy="nxt_wiki_kategorien"
data-term="<?php echo esc_attr($term->slug); ?>">
<?php echo esc_html($term->name); ?>
</a></p>
</div>
<?php
}
$is_child = true;
}
}
?>
<div class="relevanssi-live-search-result wiki<?php echo $is_child ? ' is-child' : ''; ?>" role="option" aria-selected="false">
<p>
<span class="post-meta"><?php echo get_the_date('d. M Y'); echo ' - von '; echo get_the_author(); ?></span>
<a href="<?php echo esc_url(get_permalink()); ?>">
<?php the_title(); ?>
</a>
</p>
</div>
<?php
}
function render_project_result($post) {
$is_child = get_post_status() === 'publish' && get_post_field('post_parent') > '0';
?>
<div class="relevanssi-live-search-result projects<?php echo $is_child ? ' is-child' : ''; ?>" role="option" aria-selected="false">
<p>
<span class="post-meta"><?php echo get_the_date('d. M Y', $post->ID); echo ' - von '; echo get_the_author(); ?></span>
<a href="<?php echo esc_url(get_permalink($post)); ?>"
class="project-search-link"
data-post-id="<?php echo esc_attr($post->ID); ?>"
data-post-type="projects"
data-module="projects"
data-search-term="<?php echo esc_attr(get_search_query()); ?>">
<?php echo esc_html(get_the_title($post)); ?>
</a>
</p>
</div>
<?php
}
function render_default_result($post, $post_counter) {
$is_child = get_post_status() === 'publish' && get_post_field('post_parent') > '0';
$post_type = get_post_type($post);
?>
<div class="relevanssi-live-search-result <?php echo esc_attr($post_type); echo $is_child ? ' is-child' : ''; ?>" role="option" aria-selected="false">
<p>
<span class="post-meta"><?php echo get_the_date('d. M Y', $post->ID); echo ' - von '; echo get_the_author(); ?></span>
<a href="<?php echo esc_url(get_permalink($post)); ?>"
class="ajax-post-link"
data-post-id="<?php echo esc_attr($post->ID); ?>"
data-post-type="<?php echo esc_attr($post_type); ?>"
data-module="news">
<?php echo esc_html(get_the_title($post)); ?>
</a>
</p>
</div>
<?php
}
function render_user_result($post) {
// Get user ID from post object
$user_id = $post->user_id ?? null;
if (!$user_id) {
error_log('User ID not found in search result post object');
return;
}
// Get actual user object
$user = get_user_by('ID', $user_id);
if (!$user) {
error_log('User not found for ID: ' . $user_id);
return;
}
<div class="relevanssi-live-search-result user fallback" role="option" aria-selected="false">
<div class="user-result-container">
<div class="user-avatar">
<?php echo get_avatar($user->ID, 50); ?>
</div>
<div class="user-info">
<a href="<?php echo esc_url('/?module=colleagues&show_user=' . $user->ID); ?>"
class="colleague-link"
data-user-id="<?php echo esc_attr($user->ID); ?>"
data-search-term="<?php echo esc_attr(get_search_query()); ?>">
<?php echo esc_html($user->display_name); ?>
</a>
<div class="user-meta">
<?php
$position = get_user_meta($user->ID, 'nxt_job_position', true);
if ($position) : ?>
<span class="user-position"><?php echo esc_html($position); ?></span>
<?php endif;
$departments_raw = get_user_meta($user->ID, 'nxt_job_departments_repeater', true);
$departments = [];
if (!empty($departments_raw)) {
$unserialized = maybe_unserialize($departments_raw);
if (is_array($unserialized)) {
foreach ($unserialized as $dept_item) {
if (!empty($dept_item['nxt_job_department_single'])) {
$departments[] = $dept_item['nxt_job_department_single'];
}
}
}
}
if (!empty($departments)) : ?>
<span class="user-department"><?php echo esc_html(implode(', ', $departments)); ?></span>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
return;
}
}
function separate_attachments($posts) {
$regular_posts = array();
$image_attachments = array();
$other_attachments = array();
foreach ($posts as $post) {
if (get_post_type($post) === 'attachment') {
if (wp_attachment_is('image', $post->ID)) {
$image_attachments[] = $post;
} else {
$other_attachments[] = $post;
}
} else {
$regular_posts[] = $post;
}
}
return array(
'regular_posts' => $regular_posts,
'image_attachments' => $image_attachments,
'other_attachments' => $other_attachments
);
}
// Main template logic
if (have_posts()) : ?>
<div class="search-results-container small_container">
<?php
$separated_posts = separate_attachments($wp_query->posts);
$wp_query->posts = array_merge(
$separated_posts['regular_posts'],
$separated_posts['image_attachments'],
$separated_posts['other_attachments']
);
$custom_post_type_labels = get_custom_post_type_labels();
$found_post_types = get_found_post_types($wp_query->posts);
render_search_index($found_post_types, $custom_post_type_labels);
$status_element = render_status_element($wp_query);
$status_location = apply_filters('relevanssi_live_search_status_location', 'before');
if ('before' === $status_location) {
echo $status_element;
}
$current_post_type = '';
$displayed_terms = array();
$post_counter = 0;
$in_attachment_section = false;
echo '<div class="search-results-type-container">';
while (have_posts()) : the_post();
$post_type = get_post_type();
$is_image = $post_type === 'attachment' && wp_attachment_is('image', $post->ID);
if ($post_type === 'nxt_wiki_kategorien') continue;
if ($post_type === 'attachment' && !$in_attachment_section) {
$in_attachment_section = true;
if ($is_image) {
$display_type = 'attachment_image';
$container_class = ' pswp-gallery';
} else {
$display_type = 'attachment_other';
$container_class = '';
}
} elseif ($post_type === 'attachment' && $in_attachment_section) {
if ($is_image && $current_post_type !== 'attachment_image') {
$display_type = 'attachment_image';
$container_class = ' pswp-gallery';
} elseif (!$is_image && $current_post_type !== 'attachment_other') {
$display_type = 'attachment_other';
$container_class = '';
} else {
$display_type = $current_post_type;
$container_class = $is_image ? ' pswp-gallery' : '';
}
} else {
$display_type = $post_type;
$container_class = '';
}
if ($display_type !== $current_post_type) {
if ($post_counter > 5) {
echo '</details>';
}
if ($current_post_type !== '') {
echo '</div><div class="search-results-type-container' . $container_class . '">';
}
$post_type_label = isset($custom_post_type_labels[$display_type])
? $custom_post_type_labels[$display_type]
: get_post_type_label($display_type);
$post_type_class = sanitize_html_class('search-results-type-heading--' . $display_type);
?>
<h2 id="<?php echo esc_attr($post_type_class); ?>" class="search-results-type-heading <?php echo esc_attr($post_type_class); ?>">
<?php echo wp_kses($post_type_label, array('i' => array('class' => array()))); ?>
</h2>
<?php
$current_post_type = $display_type;
$post_counter = 0;
}
$post_counter++;
if ($post_counter == 6) {
echo '<details class="search-results-details">
<summary class="search-results-summary">' . __('Show more results', 'hoenninger-intranet') . '</summary>';
}
switch ($display_type) {
case 'attachment_image':
case 'attachment_other':
render_attachment_result($post);
break;
case 'wiki':
render_wiki_result($post, $displayed_terms);
break;
case 'user':
render_user_result($post);
break;
case 'projects':
render_project_result($post);
break;
default:
render_default_result($post, $post_counter);
break;
}
endwhile;
if ($post_counter > 5) {
echo '</details>';
}
if ('after' === $status_location) {
echo $status_element;
}
?>
</div>
<?php else : ?>
<div class="search-results-container small_container">
<p class="relevanssi-live-search-no-results" role="status">
<?php esc_html_e('No results found.', 'relevanssi-live-ajax-search'); ?>
</p>
<?php
if (function_exists('relevanssi_didyoumean')) {
relevanssi_didyoumean(
$wp_query->query_vars['s'],
'<p class="relevanssi-live-search-didyoumean" role="status">' . __('Did you mean', 'relevanssi-live-ajax-search') . ': ',
'</p>'
);
}
?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment