php - 自定义帖子标题和图片在主页上不起作用 - 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 »</a></p></dd> -->
<?php endforeach;
wp_reset_postdata();
}
add_shortcode( 'show-credit-cards-blocks', 'show_credit_cards_posts' );
解决方案
使用 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 »</a></p></dd> -->
<?php
}
wp_reset_query();
?>
推荐阅读
- vim - 即使在后面的一行,也要转到下一个字符?
- android - Android检查应用程序是否在调试器上暂停
- android - 从 android 应用程序连接到本地后端
- apache-kafka - 监控 Kafka/Confluent JMX 指标的警报管理阈值
- javascript - 如何使用 localstorage 将动态 Json 值存储在单个键中?
- c# - 在数组c#的现有会话中添加字符串数组
- arrays - 数组 .map() 返回未定义
- python - 如何在 tensorflow 中使用 eval() 提取值以及我们需要启动会话的次数
- c# - ElasticSearch:将 MultiLineString 映射到 .Net 中的嵌套对象
- ios - NSKeyValueObservation:无法从对象中删除键路径的观察者,因为它没有注册为观察者