2017-05-07 116 views
0

我是WordPress新手,已成功檢索每個類別的三個帖子並將其列出。我也想爲每個帖子檢索精選圖片。我在index.php文件中嘗試了以下代碼。代碼檢索了我提到的三個帖子,但檢索圖片的代碼只是在網站上以純文本的形式出現,所以我認爲我有一些語法錯誤。誰能幫我嗎?任何幫助表示讚賞。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

請使用下面的代碼,你if (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(). 
?> 
之前錯過 <?php
相關問題