对不起,我对WordPress编码很新,从头学习它。请原谅我缺乏经验。
我有一个名为“产品”的自定义帖子类型,并希望在主页中仅显示它们作为基于日期的存档。但是,我不希望显示帖子标题或帖子中的任何其他内容,除了该日期前三或四个帖子的特色。像这样:
我正在尝试以下代码,但它将帖子作为正常循环返回。
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_day = get_the_time('d');
?>
<div class="idpostdate">
<a href="<?php echo get_day_link( $archive_year, $archive_month, $archive_day); ?>"><?php the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?></a>
</div>
<div class="thumbnail">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
任何指导?
答案 0 :(得分:1)
the_ID()
和the_post_thumbnail()
等功能会在主查询上运行。如果您想在代码中使用其等效代码,则需要在$query->
前加上它们。
未经测试,但我认为它会做你想要的:
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php $query->the_ID(); ?>" <?php $query->post_class(); ?>>
<?php
$archive_year = $query->get_the_time('Y');
$archive_month = $query->get_the_time('m');
$archive_day = $query->get_the_time('d');
?>
<div class="idpostdate">
<a href="<?php echo get_day_link( $archive_year, $archive_month, $archive_day); ?>"><?php $query->the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?></a>
</div>
<div class="thumbnail">
<?php if ( $query->has_post_thumbnail() ) { $query->the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>