Skip to content

Instantly share code, notes, and snippets.

@Hotfirenet
Last active February 23, 2017 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hotfirenet/3d7aa2752e2893b214e467461d5b1427 to your computer and use it in GitHub Desktop.
Save Hotfirenet/3d7aa2752e2893b214e467461d5b1427 to your computer and use it in GitHub Desktop.
Shortcode pour wordpress qui permet de lister les articles et / ou les custom post type publiés entre 2 dates.
<?php
//By: Hotfirenet
//Description Shortcode: List post, cpt, published between 2 dates
//Use: [recap_article type="post,guide" datedebut="2016-01-31" datefin="2016-02-28" limit=5]
//For: domadoo blog
add_shortcode( 'recap_article', function( $atts ) {
extract( shortcode_atts( array(
'type' => 'post',
'datedebut' => date('Y-m-d'),
'datefin' => '2016-01-02',
'limit' => 10,
), $atts ) );
if ( strpos( $type, ',' ) !== false )
$type = explode( ',', $type )
$datedebut = explode( '-', $datedebut );
$datefin = explode( '-', $datefin );
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'date_query' => array(
array(
'after' => array('year' => $datedebut[0],
'month' => $datedebut[1],
'day' => $datedebut[2],
),
'before' => array('year' => $datefin[0],
'month' => $datefin[1],
'day' => $datefin[2],
),
'inclusive' => true,
),
),
'posts_per_page' => $limit,
);
$wp_query = new WP_Query( $args );
ob_start();
echo '<div id="recap-article">';
if($wp_query->have_posts()) : while ($wp_query->have_posts() ) : $wp_query->the_post();
global $post;
echo '<a href="'. $post->guid .'"><img src="'. get_the_post_thumbnail_url($post->ID) .'"></a>';
echo '<h2><a href="'. $post->guid .'">'. $post->post_title .'</a></h2>';
echo '<p>'. get_the_excerpt() .'</p>';
endwhile; endif;
wp_reset_postdata();
echo '</div>';
return ob_get_clean();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment