wir
6/16/2016 - 6:38 PM

WordPress Loop with Google Structured Data

WordPress Loop with Google Structured Data

<?php
// checks if there are any posts that match the query
if ( have_posts() ) :

// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post(); ?>

<article id="post-<?php the_ID(); ?>" role="article" itemprop="hasPart" itemscope="" itemtype="http://schema.org/Article">
<meta itemscope='itemscope' itemprop='mainEntityOfPage' itemType='https://schema.org/WebPage'/>

    <!-- 1. Title Start -->
    <h1 itemprop="headline">
        <a href="<?php the_permalink(); ?>" itemprop="url">
            <?php the_title() ;?>
        </a>
    </h1>
    <!-- 1. Title End -->

    <!-- 2. Post Meta Start -->
    <p>
        <!-- 2.1 Published date Start -->
        Published on <span datetime="<?php echo the_time('c'); ?>" itemprop="datePublished" content="<?php echo the_time('c'); ?>">
            <?php the_time('M j, Y'); ?>
        </span>
        <!-- 2.1 Published date End -->
        
        <!-- 2.2 Author Start -->
        by <span class="post-author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person">
        <span itemprop="name"><?php the_author(); ?></span>
        </span>
        <!-- 2.2 Author End -->
        
        <!-- 2.3 Category Start -->
        in <?php the_category(', '); ?>
        <!-- 2.3 Category End -->
    </p>
    <!-- 2. Post Meta End -->
    
    <!-- 3. Thumbnail Start -->
    <?php if ( has_post_thumbnail() ) { ?>
    <span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
        <a href="<?php the_permalink(); ?>" rel="bookmark">
            <?php the_post_thumbnail('medium'); ?>
        </a>

        <?php
        $thumb_id = get_post_thumbnail_id();
        $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true);
        $thumb_url = $thumb_url_array[0];
        ?>
        <meta itemprop="url" content="<?php echo $thumb_url ?>">
        <meta itemprop='width' content='569'/>
        <meta itemprop='height' content='309'/>  
    </span>
    <?php } ?>
    <!-- 3. Thumbnail End -->
    
    <!-- 4. Content / Excerpt Start -->
    <span itemprop="description">
        <?php the_excerpt(); ?>
    </span>
    <!-- 4. Content / Excerpt End -->

    <div class="publisher-img" itemprop="publisher" itemscope itemtype="https://schema.org/Organization">
    <div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
      <img src="http://ahmedkaludi.com/wp-content/uploads/2016/05/ak-logo.png" width="396" height="91"/>
      <meta itemprop="url" content="http://ahmedkaludi.com/wp-content/uploads/2016/05/ak-logo.png">
      <meta itemprop="width" content="232">
      <meta itemprop="height" content="90">
    </div>
    <meta itemprop="name" content="Ahmed Kaludi">
    </div>
    <meta itemprop="dateModified" content="<?php echo the_time('c'); ?>"/>
    
</article>

<?php
// Stop the loop when all posts are displayed
endwhile; ?>

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else: // If no posts were found ?>
    <p>Sorry, this post does not exist</p>
<?php endif; // End Loop ;) ?>