yujiokayama2
5/20/2017 - 3:33 AM

人気記事を表示する

人気記事を表示する

//Output
<ul class="list-sytle-bb-dashed">
<?php
setPostViews(get_the_ID());
$args = array( 'meta_key' =>'post_views_count', 'orderby' =>'meta_value_num', 'order' =>'DESC', 'post_type' => 'post', 'posts_per_page' => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

        <li class="mb10 cf">
            <a href="<?php the_permalink(); ?>">

            <div class="flL w80 mr10">
            <?php if( has_post_thumbnail() ): ?>
                <?php the_post_thumbnail( 'side-cont-thumbs' ); ?>
            <?php endif; ?>
            </div>
            
            <dl class="flL w140">
                <dt><?php echo do_shortcode('[cat_ico]'); ?></dt>
                <dd class="mt5"><?php the_time("Y.m.d"); ?><?php if( time() - get_the_time('G') < 604800 ): ?><div class="new-entry">\NEW!/</div><?php endif; ?></dd>
                <dd class="mt5"><?php echo mb_strimwidth(get_the_title(), 0, 24, "…", "UTF-8"); ?></dd>
            </dl>
            <i class="fa fa-angle-right angle-right"></i>
            </a>            
        </li>
<?php endforeach; 
wp_reset_postdata();?>
</ul>


//functions.php

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
    }else{
            $count++;
            update_post_meta($postID, $count_key, $count);
    }
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);