WordPress - 检索按类别列出的帖子的精选图像

时间:2017-05-07 07:40:40

标签: php wordpress

我是WordPress的新手,并成功检索了每个类别的三个帖子并列出了它们。我还想检索每个帖子的精选图片。我在index.php文件中尝试了以下代码。正如我所提到的,代码检索了三个帖子,但检索图像的代码只是在网站上以纯文本形式出现,所以我假设我有一些语法错误。谁能帮我吗?任何帮助表示赞赏。

的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().
?>

1 个答案:

答案 0 :(得分:2)

请使用以下代码,您在... var spriteurl = "/Pokedex/sprites/" + eachName[1]; img = document.createElement("img"); img.setAttribute("src", spriteurl); klass = ["a.png", "b.png"].some(function(e) { return e == eachName[1]; } ? "found" : "unfound"; img.setAtrribute("class", klass) // or classList += document.getElementById("pokedex-view").appendChild(img); ...

之前错过了<?php
if (has_post_thumbnail($post->ID)) {