Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Created July 23, 2014 13:37
Show Gist options
  • Save willybahuaud/1ad49811b8af2cf5abe5 to your computer and use it in GitHub Desktop.
Save willybahuaud/1ad49811b8af2cf5abe5 to your computer and use it in GitHub Desktop.
Template de recherche en ajax
<?php
$o = array();
$o[ 'title' ] = wp_title( '', false ); //me sert pour mettre à jour le titre de la fenêtre de navigation
$o[ 'type' ] = 'liste'; // le type du template
$o[ 'breadcrumb' ] = ffeeeedd__ariane(); //me sert pour mettre à jour le fil d'ariane
$q = get_search_query();
if( have_posts() ) :
global $wp_query;
$f = $wp_query->found_posts;
$o[ 'intro' ] = '<div class="grille-intro to-be-added"><h1>' . wp_sprintf( _n( '1 résultat pour la recherche &laquo;%2$s&raquo;', '%1$d résultats pour la recherche &laquo;%2$s&raquo;', $f, 'nebula' ), $f, $q ) . '</h1></div>';
$article = '<article class="grille to-be-added">';
while( have_posts() ) : the_post();
if( has_post_thumbnail() ) {
$article .= '<a href="' . get_permalink() . '" class="lien-article">';
$c = ( $c = get_comments_number( $post->ID ) ) ? '<div class="comments-number">' . $c . '</div>' : '';
$article .= the_title( '<div class="titre-lien-article"><h2>', '</h2>' . $c . '</div>', false );
$article .= get_the_post_thumbnail( $post->ID, 'article', array( 'class' => 'image-article' ) );
$article .= '<div class="extract-article">';
$article .= wp_trim_words( get_the_excerpt(), 28 );
$article .= '<div class="article-date">' . get_the_time( 'd F Y', $post ) . '</div>';
$article .= get_the_post_thumbnail( $post->ID, 'article', array( 'class' => 'background-image-article' ) );
$article .= '</div>';
$article .= '</a>';
}
endwhile;
$article .= '</article>';
$o[ 'article' ] = $article;
else :
$o[ 'intro' ] = '<div class="grille-intro"><h1>' . wp_sprintf( __( 'Aucun résultat pour la recherche &laquo;%s&raquo;', 'nebula' ), $q ) . '</h1></div>';
endif;
if( is_user_logged_in() ) {
ob_start();
wp_admin_bar_render();
$o[ 'admin_bar' ] = ob_get_clean();
}
echo json_encode( $o );
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment