Exemple #1
0
function get_related_wp_query_args()
{
    global $post;
    if (is_related_entry_association_category()) {
        //カテゴリ情報から関連記事をランダムに呼び出す
        $categories = get_the_category($post->ID);
        $category_IDs = array();
        foreach ($categories as $category) {
            array_push($category_IDs, $category->cat_ID);
        }
        if (empty($category_IDs)) {
            return;
        }
        return $args = array('post__not_in' => array($post->ID), 'posts_per_page' => intval(get_related_entry_count()), 'category__in' => $category_IDs, 'orderby' => 'rand');
    } else {
        //タグ情報から関連記事をランダムに呼び出す
        $tags = wp_get_post_tags($post->ID);
        $tag_IDs = array();
        foreach ($tags as $tag) {
            array_push($tag_IDs, $tag->term_id);
        }
        if (empty($tag_IDs)) {
            return;
        }
        return $args = array('post__not_in' => array($post->ID), 'posts_per_page' => intval(get_related_entry_count()), 'tag__in' => $tag_IDs, 'orderby' => 'rand');
    }
}
Exemple #2
0
<?php

//カテゴリ情報から関連記事を10個ランダムに呼び出す
$categories = get_the_category($post->ID);
$category_ID = array();
foreach ($categories as $category) {
    array_push($category_ID, $category->cat_ID);
}
$args = array('post__not_in' => array($post->ID), 'posts_per_page' => intval(get_related_entry_count()), 'category__in' => $category_ID, 'orderby' => 'rand');
$query = new WP_Query($args);
?>
  <?php 
if ($query->have_posts()) {
    ?>
  <?php 
    while ($query->have_posts()) {
        $query->the_post();
        ?>
    <div class="related-entry">
      <div class="related-entry-thumb">
  <a href="<?php 
        the_permalink();
        ?>
" title="<?php 
        the_title_attribute();
        ?>
">
        <?php 
        if (has_post_thumbnail()) {
            // サムネイルを持っているとき
            ?>