我应该如何将此自定义查询实现到 index.php 中


How am I supposed to implement this custom query into index.php?

以下是我的索引.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();
}

但是,如果没有将其作为明文出现在我的主页上或发现自己处于某种语法错误中,我就未能正确执行此操作。

问题是我无法获取我在索引代码中标记的元素.php并在循环后启动以使用自定义查询

这里更简洁的方法是通过loop_start操作添加自定义查询。这样做的好处是你根本不需要更改模板文件。您可以简单地将代码放在函数中.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();
    }
});