以下是我的index.php文件
<?php get_header(); ?>
<?php
if(get_option('zens_home') == "blog") { ?>
<?php include (TEMPLATEPATH . '/lib/blog-home.php'); ?>
<?php } else { ?>
<div id="home-content" class="clearfix">
<ul id="shelf">
<?php
?>
<?php while (have_posts()) : the_post(); ?>
/*Start of mark*/
<li class="box" id="post-<?php the_ID(); ?>">
<?php $disc = get_post_meta($post->ID, 'fabthemes_discount', true); ?>
<?php if ( $disc ) { ?>
<span class="salebadge"></span>
<?php }?>
<a href="<?php the_permalink() ?>"><img class="productshot" width="172" height="243" src="<?php get_image_url(); ?>" alt="<?php the_title(); ?>"/></a>
<div class="pricetab clearfix">
<?php if ( $disc ) { ?>
<span class="oldprice"><del> <?php echo get_post_meta($post->ID, 'fabthemes_disc-price', true) ?> </del></span>
<?php }?>
<span class="prodetail"><a href="<?php the_permalink() ?>"><?php $price=get_post_meta($post->ID, 'fabthemes_price', true); echo $price; ?></a></span>
</div>
</li>
/*End of mark*/
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
</ul>
</div>
在它已经使用的循环之上,我还想放置以下自定义查询
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = new WP_Query( $args );
// Run the loop
if ( $vip_posts->have_posts() ) {
while ( $vip_posts->have_posts() ) {
$vip_posts->the_post();
// Display what you want in your loop like
echo '</br>';
}
wp_reset_postdata();
}
但是,如果没有将其作为我主页上的明文或者发现自己出现某种语法错误,我就无法正常使用。
问题是我无法获取我在index.php的代码中标记的元素,并在使用自定义查询的循环之后启动
答案 0 :(得分:1)
此处更简洁的方法是通过loop_start
操作添加自定义查询。这样做的好处是您根本不需要更改模板文件。如果你愿意,你可以简单地将你的代码放在functions.php或插件中(我总是喜欢)
另一个优点是循环彼此分开,根据另一个问题你需要的东西,所以这种方法非常适合
以下代码未经测试,但应该让您了解如何使用操作
add_action( 'loop_start', function ( \WP_Query $q )
{
// Make sure we only target the main query, if not, bail
if ( !$q->is_main_query() )
return;
// Set a static counter to make sure this runs only once to avoid infinitive loops
static $count = 0;
// Make sure our counter is 0, if not, bail
if ( 0 != $count )
return;
// Update our counter
$count++;
// This is the main query, lets target specific pages only
if ( $q->is_home()
|| $q->is_post_type_archive( 'products' )
|| $q->is_tax( 'product-category' )
) {
// Bail on vip pages
if ( $q->is_tax( 'product-category', 'vip' ) ) // Make sure about vip slug
return;
// Bail if this is not the first page
if ( $q->is_paged() )
return;
// Lets add our custom loop
global $wp_query;
$args = [
'posts_per_page' => -1,
'post_type' => 'products',
'orderby' => 'rand', // Order these posts randomly
'tax_query' => [
[
'taxonomy' => 'product-category',
'terms' => 27
]
]
];
$vip_posts = new WP_Query( $args );
// Run the loop
if ( $vip_posts->have_posts() ) {
while ( $vip_posts->have_posts() ) {
$vip_posts->the_post();
// Display what you want in your loop like
the_title();
echo '</br>';
}
wp_reset_postdata();
}
// Lets make sure our main loop counter starts at -1
$wp_query->rewind_posts();
}
});