【问题标题】:Custom Post title and picture is not working at Home page - Wordpress自定义帖子标题和图片在主页上不起作用 - Wordpress
【发布时间】:2018-06-12 04:18:26
【问题描述】:

我有一个获取自定义帖子标题和图片的功能。当我将它放在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' );

【问题讨论】:

  • 在主页上 $custom_query 的结果是什么?

标签: php wordpress


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    相关资源
    最近更新 更多