ArnoSr
2/19/2020 - 12:55 PM

Wordpress - Elements utiles de base

<?php
/* Return the post thumbnail URL. */
get_the_post_thumbnail_url( int|WP_Post $post = null, string|array $size = 'post-thumbnail' )


/* Retrieve theme directory URI. */
get_template_directory_uri()


/* Template part */
set_query_var('var', $var);
get_template_part( 'loop-templates/content', 'post-home' );


// Image sizes
add_action( 'after_setup_theme', 'wpm_new_image' );
function wpm_new_image(){
  // L'image sera tronquée exactement à la dimension indiquée
  add_image_size( 'wpm_taille_1', 200, 200, true ); 
  // L'image sera redimensionnée de manière proportionnelle à la largeur indiquée (ici 600px)
  // La hauteur peut-etre différente d'une image à l'autre pour garder la proportion originale
  add_image_size( 'wpm_taille_2', 600, 300 ); 
  // Utile si vous publiez de très grande image type infographie.
  add_image_size( 'wpm_taille_3', 590, 9999 );
}

/* Retrieves the post excerpt. */
get_the_excerpt(); // !!! Attention au code par défaut de understrap 


/**
 * WP Custom Excerpt Length Function
 * Place in functions.php
 * This example returns ten words, then [...]
 * Manual excerpts will override this
 */
add_filter( 'excerpt_length', 'ld_custom_excerpt_length', 999 );
function ld_custom_excerpt_length( $length ) {
	return 20;
}


/**
 * @param $more
 * @return string
 *
 * Replace or Remove link Read more  of excerpt
 */
add_filter('excerpt_more', 'custom_excerpt_more_link');
function custom_excerpt_more_link($more){
	return ''; // '<a href="' . get_the_permalink() . '" rel="nofollow">&nbsp;[more]</a>';
}



/* Get Posts */
$argsLastPost = array(
    'post_type' => array('post'),
    'posts_per_page' => 9,
    'orderby' => 'date',
    'order' => 'DESC',
    'page' => 1
);
$wp_query = new WP_Query($argsLastPost);

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



/* Destroys the previous query and sets up a new query. */
wp_reset_query();


// Retrieve the terms of the taxonomy that are attached to the post.
get_the_terms( int|WP_Post $post, string $taxonomy );


// Redirect custom_post
add_action( 'template_redirect', 'redirect_custom_post' );
function redirect_custom_post() {
	$queried_post_type = get_query_var('post_type');
	if ( is_single() && 'custom_post' ==  $queried_post_type ) {
		wp_redirect( home_url(), 301 );
		exit;
	}
}



/* Disable Admin Bar */
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
	if (!current_user_can('administrator') && !is_admin()) {
	  show_admin_bar(false);
	}
}


// Retrieves all menu items of a navigation menu.
wp_get_nav_menu_items( int|string|WP_Term $menu, array $args = array() );