Skip to content

Instantly share code, notes, and snippets.

@rccc
Last active December 3, 2015 20:58
Show Gist options
  • Select an option

  • Save rccc/201f0bfce942a91fc4a4 to your computer and use it in GitHub Desktop.

Select an option

Save rccc/201f0bfce942a91fc4a4 to your computer and use it in GitHub Desktop.

Afficher les champs personnalisés :

<?php 
	$data_concert = get_post_meta(get_the_id(), 'date_concert' ); 
	if(!empty($date_concert)):
		//121012016
		$str = preg_replace('/(\d{2})(\d{2})(\d{4})/i', '$3$2$1', $date_concert);
		$date = DateTime::createFromFormat('dmY', $str);
	?>
	<div>
		<span class="date-concert day"><?php echo $date->format('d') ?></span>
		<span class="date-concert month"><?php echo $date->format('m') ?></span>
	/div>
?>

Il est préférable d'insérer ce code dans un fragment de template nommé postmeta-concert.php et appeler ce fragment de template avec la fonction *get_template_part'.

	get_template_part('postmeta-concert');

Activer la navigation principale

Nous avons déjà abordé la notion de fonctionnalités des thèmes ( theme features ) proposées par WP et qui sont activables lors de la création du nouveau thème.

Le menu de navigation fait partie de ces fonctionnalités, vous devez donc l'activer :

function my_theme_setup() {

	add_theme_support( 'post-thumbnails' ); 

	register_nav_menu( 'primary', __( 'Navigation Menu', 'my_theme' ) );
	/*
	 * This theme uses a custom image size for featured images, displayed on
	 * "standard" posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );
	
}
//cette action intervient une fois que WP a determiné le thème à activer
add_action( 'after_setup_theme', 'twentythirteen_setup' );

Ensuite, dans le fichier header.php, vous devrez ajouter le tag suivant en vous aidant de cet exemple : [header.php] (https://github.com/WordPress/WordPress/blob/master/wp-content/themes/twentythirteen/header.php)

  <?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

Notez la présence des marqueurs de template : bloginfo(), wp_title(), wp_head()... Vous devrez les intégrez à votre fichier header.php.

Ajouter de nouvelles fonctionnalités :

function my_theme_setup() {

	add_theme_support( 'post-thumbnails' ); 

	//set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)
        //add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
        
        
	register_nav_menu( 'primary', __( 'Navigation Menu', 'my_theme' ) );
	/*
	 * This theme uses a custom image size for featured images, displayed on
	 * "standard" posts and pages.
	 */

	$args = array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption'
	);
	add_theme_support( 'html5', $args );

}

Création d'un nouveau type de contenu 'Annonce' ou 'Concert'.

function my_theme_init(){

register_post_type(
  'annonce',
  array(
    'label' => 'Annonces',
    'labels' => array(
      'name' => 'Annonces',
      'singular_name' => 'Annonce',
      'all_items' => 'Toutes les annonces',
      'add_new_item' => 'Ajouter une annonces',
      'edit_item' => "Éditer l'annonces",
      'new_item' => 'Nouvelle annonce',
      'view_item' => "Voir l'annonce",
      'search_items' => 'Rechercher parmi les annonces',
      'not_found' => 'Aucune annonce trouvée',
      'not_found_in_trash'=> 'Pas d'annonce dans la corbeille'
      ),
    'public' => true,
    'capability_type' => 'post',
    'supports' => array(
      'title',
      'editor',
      'thumbnail'
    ),
    'has_archive' => true
  )
);

}
add_action('init', 'my_theeme_init');

Création du template 'archives' avec pagination

Ce template va lister toutes les annonces publiées ou tous les concerts à venir. Ce qu'il faut :

  • Pour les concerts : filtrer la requête pour n'afficher que les concerts à venir :). A faire un peu plus tard,
  • Une boucle pour afficher la liste des resultats,
  • une pagination si plusieurs pageq de résultats.

Boucle

Adapter cette boucle :

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 	<!-- Display the Title as a link to the Post's permalink. -->

 	<h2>
 		<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
 			<?php the_title(); ?>
 		</a>
 	</h2>

 	<!-- Display the Post's content in a div box. -->
 	<div class="entry">
 		<?php the_content(); ?>
 	</div>

 	<!-- Display a comma separated list of the Post's Categories. -->
 	<p class="postmetadata">
 		<?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?>
 	</p>

 	<!-- Stop The Loop (but note the "else:" - see next line). -->

 <?php endwhile; else : ?>

 	<!-- The very first "if" tested to see if there were any Posts to -->
 	<!-- display.  This "else" part tells what do if there weren't any. -->
 	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

 	<!-- REALLY stop The Loop. -->
 <?php endif; ?>

pagination

Vous pouvez reprendre le conde suivant :

global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
	'format' => '?paged=%#%',
	'current' => max( 1, get_query_var('paged') ),
	'total' => $wp_query->max_num_pages
) );

Liens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment