I am new to WordPress and have successfully retrieved three posts for every category and listed them. I also want to retrieve the featured image for every post. I have tried out the following code in the index.php
file. The code retrieved the three posts as I have mentioned, but the code to retrieve the images just appeared as plain text on the website, so I assume I have some syntax error. Can anyone help me out? Any help is appreciated.
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().
?>
Please use below code, you have missed <?php
before 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().
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.