自定义帖子标题和图片无法在主页 - Wordpress

时间:2018-06-12 04:18:26

标签: php wordpress

我有一个功能来获取自定义帖子标题和图片。当我将它放在header.php中时,它应该可以正常工作。出于某种原因,当我将短代码放入Wordpress主页时,它显示没有图片的“HomePage”。

如何在主页中显示自定义帖子标题和图片?

function show_credit_cards_posts() {
    $custom_query = get_posts( array(
        'post_type'      => 'credit-cards',
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
        )
    );

    foreach ($custom_query as $post) :
        setup_postdata($post); ?>

  <div class="top-button">
      <div class="img-holder"><?php echo get_the_post_thumbnail(); ?></div>
      <?php the_title(); ?>
  </div>

  <!-- <p><a class="more" href="<?php // echo get_post_permalink();?>">Find out more &raquo;</a></p></dd> -->
  <?php endforeach;
  wp_reset_postdata();
}

add_shortcode( 'show-credit-cards-blocks', 'show_credit_cards_posts' );

1 个答案:

答案 0 :(得分:0)

使用wp_query

     <?php
            $query = new WP_Query(array(
                'post_type' => 'credit-cards',
                'post_status' => 'publish',
                'posts_per_page' => -1
            ));
            while ($query->have_posts()) {
                $query->the_post();

        ?>


    <div class="top-button">
      <div class="img-holder"><?php echo get_the_post_thumbnail(); ?></div>
      <?php get_the_title(); ?>
  </div>

  <!-- <p><a class="more" href="<?php // echo get_post_permalink();?>">Find out more &raquo;</a></p></dd> -->
        <?php
            } 
            wp_reset_query();
        ?>