1
我有一個WooCommerce商店,我想顯示如下(按順序)中的一個特色形象&標題:如何編寫空wp_query更高效的回退在WordPress的
- 特色產品
- 如果沒有特色產品,然後粘後
- 如果沒有粘後,則最近張貼
但我也想寫高效的代碼。我如何簡化這個並刪除多餘的PHP和HTML?
/* START FEATURED PRODUCT QUERY */
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_query' => array(
'key' => '_featured',
'value' => 'yes'
),
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>');
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
} else {
/* START FALLBACK POST QUERY */
$args = array(
'posts_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'ignore_sticky_posts' => 1
);
$query = new WP_Query($args);
while($query->have_posts()) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>');
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
}
第二WP_Query具有完全相同的HTML輸出,只是不同$args