2017-05-07 22 views
0

私はWordPressを初めて使用しており、すべてのカテゴリに対して3つの投稿を正常に取得してリストしました。私はまたすべてのポストのために特色のイメージを取り戻したいと思う。私はindex.phpファイルの次のコードを試しました。私が言及したように、コードは3つの投稿を取得しましたが、画像を取得するコードはWebサイトにプレーンテキストとして現れました。したがって、私は構文エラーがあると仮定します。誰か助けてくれますか?どんな助けもありがとうございます。WordPress - カテゴリ別に掲示された投稿の特集画像を取得する

のindex.php

<?php 
//get all terms (e.g. categories or post tags), then display all posts in each term 
$taxonomy = 'category';// e.g. post_tag, category 
$param_type = 'category__in'; // e.g. tag__in, category__in 
$term_args=array(
    'orderby' => 'name', 
    'order' => 'ASC' 
); 
$terms = get_terms($taxonomy,$term_args); 
if ($terms) { 
    foreach($terms as $term) { 
    $args=array(
     "$param_type" => array($term->term_id), 
     'post_type' => 'post', 
     'post_status' => 'publish', 
     'posts_per_page' => 3, 
     'caller_get_posts'=> 1, 
    ); 
    $my_query = null; 
    $my_query = new WP_Query($args); 
    if($my_query->have_posts()) { 
     echo 'List of Posts in '.$taxonomy .' '.$term->name; 
     while ($my_query->have_posts()) : $my_query->the_post(); ?> 
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 



if (has_post_thumbnail($post->ID)) { 
     $retina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'homepage-thumb-retina'); 
     echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ; 
}; 

     <?php 
     endwhile; 
    } 
    } 
} 
wp_reset_query(); // Restore global post data stomped by the_post(). 
?> 

答えて

2

コードの下に使用してください、あなたは<?phpif (has_post_thumbnail($post->ID)) {

<?php 
//get all terms (e.g. categories or post tags), then display all posts in each term 
$taxonomy = 'category';// e.g. post_tag, category 
$param_type = 'category__in'; // e.g. tag__in, category__in 
$term_args=array(
    'orderby' => 'name', 
    'order' => 'ASC' 
); 
$terms = get_terms($taxonomy,$term_args); 
if ($terms) { 
    foreach($terms as $term) { 
    $args=array(
     "$param_type" => array($term->term_id), 
     'post_type' => 'post', 
     'post_status' => 'publish', 
     'posts_per_page' => 3, 
     'caller_get_posts'=> 1, 
    ); 
    $my_query = null; 
    $my_query = new WP_Query($args); 
    if($my_query->have_posts()) { 
     echo 'List of Posts in '.$taxonomy .' '.$term->name; 
     while ($my_query->have_posts()) : $my_query->the_post(); ?> 
     <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> 


<?php 
if (has_post_thumbnail($post->ID)) { 
     $retina = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'homepage-thumb-retina'); 
     echo '<img src="' . $retina[0] . '" alt="' . the_title() . '" width="24" height="24" />' ; 
}; 

     endwhile; 
    } 
    } 
} 
wp_reset_query(); // Restore global post data stomped by the_post(). 
?> 
を逃しました
関連する問題