X

Shortcode avec Micro Formatted Loop, Meta, Category, Pagination

Snippets by IsItWP

L’utilisation d’un shortcode WordPress vous permet d’afficher des articles de blog individuels basés sur l’ID, le dernier nombre d’articles de blog d’une certaine catégorie ou de toutes les catégories, la pagination optionnelle, l’affichage optionnel d’un extrait ou du contenu complet et les méta optionnels.

Instructions : Ajoutez le code suivant au fichier functions.php de votre thème WordPress pour activer le nouveau shortcode.

<?php
/************* SHORTCODES ****************/
// 
function showBlogPosts( $atts ) {
	extract( shortcode_atts( array(
	// what categories to show, default = all. 
		'cat' => '',
	// number of posts
		'num' => 5,
		'p' => '',
	// The following variables work on an "on/off" switch, 1 to allow, 0 to disallow
	// excerpt or content. default = excerpt
		'excerpt' => 1,
	// show meta or not. default = yes
		'meta' => 1,
		'meta2' => 1,

	//paged. default on no. Determines if page should be paginated or not
		'pnavi' => 0
	), $atts ) );
	global $loopMeta, $loopExcerpt, $loopMeta2;
	$loopMeta = $meta;
	$loopExcerpt = $excerpt;
	$loopMeta2 = $meta2;
	if ($p != ''){
	$posts_per_page = '';	
	}
	
 wp_reset_query();
global $paged;
$args = array( 'posts_per_page' => $num, 'cat'=> $cat, 'p' => $p );
if($pnavi == 1){
$args['paged'] = get_query_var('page');
}
query_posts($args );
 while ( have_posts() ) : the_post();
/* for the loop part, use your own loop.php that contains only the post format. 
** See my "micrLoopPart.php" to see how all the options play out
*/
include(locate_template('microLoopPart.php'));
   endwhile; 

if($pnavi == 1){
echo'<div id="blogNav">';
posts_nav_link( ' ', '&raquo;', '&laquo;' );
echo '</div>';
}
 wp_reset_query();
}
add_shortcode( 'blogPosts', 'showBlogPosts' );
?>

Maintenant, créez un nouveau fichier modèle appelé microLoopPart.php dans votre dossier racine et ajoutez la boucle microformatée suivante.

<article id="post-<?php the_ID();?>" <?php post_class($classes);?> ><!-- START OF POST -->
      
      <h1 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" class="url">
        <?php the_title(); ?>
        </a></h1>
      <span class="meta vcard" style=" <?php if($loopMeta == 0){ echo "display:none"; }?> ">
      <time class="updated" datetime="<?php

$postDate = get_the_date('c');

$postDate2 = get_the_date('d.m.Y');

echo $postDate ?>" pubdate> <?php echo $postDate2; ?></time>
      | <span class="byline fn author">
      <?php the_author_posts_link(); ?>
      </span> | <a href="mailto:<?php the_author_meta('email'); ?>" class="email author">email</a> |
      <?php the_category(', '); ?>
      </span></span>

      <div class="postContent entry-content">
         <?php 
		if($loopExcerpt == 0){
		the_content();
		}
		else{
			if ( has_post_thumbnail() ) {
			 the_post_thumbnail('thumbnail', array('class' => 'alignleft')); 
	  		 
			}
		the_excerpt();
	   	echo '<div class="clearfix"></div>';
		}
		?>
        <p class="postmetadata" style=" <?php if($loopMeta2 == 0){ echo "display:none"; }?> ">Posted in
          <?php the_category(', '); ?>
          <br />
          <?php the_tags(); ?>
          <br />
          Source: <span class="vcard"><span class="source-org copyright">
          <?php bloginfo('name'); ?>
          </span></span></p>
      </div>
    </article>
<article id="post-<?php the_ID();?>" <?php post_class($classes);?> ><!-- START OF POST -->
      
      <h1 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>" class="url">
        <?php the_title(); ?>
        </a></h1>
      <span class="meta vcard" style=" <?php if($loopMeta == 0){ echo "display:none"; }?> ">
      <time class="updated" datetime="<?php

$postDate = get_the_date('c');

$postDate2 = get_the_date('d.m.Y');

echo $postDate ?>" pubdate> <?php echo $postDate2; ?></time>
      | <span class="byline fn author">
      <?php the_author_posts_link(); ?>
      </span> | <a href="mailto:<?php the_author_meta('email'); ?>" class="email author">email</a> |
      <?php the_category(', '); ?>
      </span></span>

      <div class="postContent entry-content">
         <?php 
		if($loopExcerpt == 0){
		the_content();
		}
		else{
			if ( has_post_thumbnail() ) {
			 the_post_thumbnail('thumbnail', array('class' => 'alignleft')); 
	  		 
			}
		the_excerpt();
	   	echo '<div class="clearfix"></div>';
		}
		?>
        <p class="postmetadata" style=" <?php if($loopMeta2 == 0){ echo "display:none"; }?> ">Posted in
          <?php the_category(', '); ?>
          <br />
          <?php the_tags(); ?>
          <br />
          Source: <span class="vcard"><span class="source-org copyright">
          <?php bloginfo('name'); ?>
          </span></span></p>
      </div>
    </article>

Enfin, vous devez ajouter le shortcode suivant dans vos articles de blog pour afficher une liste d’articles microformatés.

[blogPosts cat=3 excerpt=0 meta=1 meta1=0 num=2]

Vous pouvez également vérifier si le shortcode existe déjà.

Commentaires   laisser une réponse

  1. Parse error: syntax error, unexpected ‘<' in C:xampp3htdocsmrcwp-contentthemesmrcmicroLoopPart.php on line 6

  2. Hi,

    I really your code but like John said the pagination is not working. It will show the link but on next pages (like page 2) it still show the page one content. Any idea?

    Regards
    Michael

  3. Thanks for this! – Getting the_excerpt to show for custom queried posts in the sidebar on *single.php* (oddly, it worked on all other pages) was killing me.
    I tried rolling my own shortcode but i couldn’t quite get it to work. This was really handy, and fixed the problem …

    I dont know if its a bug in WP 3.3 but resetting queries, rewinding, trying three diferent query types – from wp_query to get_posts just didnt work on single.php…i was just trying to create a loop outside the main loop – and they all worked on *every page* except on single posts. So frustrating!

    thanks again!

  4. This is a great  contribution. Thank you. I have a problem though on my theme. Everything seems to work except when I add the pagination, the link is there for the pages and it goes to those pages, but every page shows the latest posts (in this example, the latest 2) This is the shortcode I used. Copied the code above without changing

    [blogPosts
    cat=3 excerpt=0 meta=1 meta1=0 num=2 pnavi=1]
    Any ideas? Using WP 3.3.1 

    Thanks

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !