首页 > 解决方案 > 自定义帖子标题和图片在主页上不起作用 - Wordpress

问题描述

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

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

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' );

标签: phpwordpress

解决方案


使用 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();
        ?>

推荐阅读