为了根据自定义字段查询产品,从插件高级自定义字段中,我必须使用此wp查询脚本来正确查询它。
但是,当我复制此脚本时,更改了所需的值,并将其插入到函数中。php导致网站崩溃。
我认为这是一个解析或语法错误,因为它使用了很多。我已经尝试删除其中一些,但仍然没有结果。当上面的所有其他脚本都被删除(不相关)后,它看起来如下所示:
<?php
<?php
add_action( \'wp_ajax_posts_by_brand_action\', \'posts_by_brand_action_callback\' );
function posts_by_brand_action_callback() {
global $wpdb; // this is how you get access to the database
$brand = $_POST[\'brand\'];
echo $whatever;
// ----- START OF BRAND FILTER CODE
// args
$args = array(
//\'numberposts\' => -1,
//\'post_type\' => \'event\',
\'meta_key\' => \'brand\',
\'meta_value\' => $brand
);
// query
$the_query = new WP_Query( $args );
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
// ------ END OF BRAND FILTER CODE
wp_die(); // this is required to terminate immediately and return a proper response
}
?>
我该怎么办?这样说是否正确,即不应在内部使用?
SO网友:amit singh
你为什么无缘无故地打开这么多php标签?以下是已审核的代码,请检查是否有效:
<?php
add_action( \'wp_ajax_posts_by_brand_action\', \'posts_by_brand_action_callback\' );
function posts_by_brand_action_callback() {
global $wpdb; // this is how you get access to the database
$brand = $_POST[\'brand\'];
echo $whatever;
// ----- START OF BRAND FILTER CODE
// args
$args = array(
//\'numberposts\' => -1,
//\'post_type\' => \'event\',
\'meta_key\' => \'brand\',
\'meta_value\' => $brand
);
// query
$the_query = new WP_Query( $args );
if($the_query->have_posts() ):
?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
// ------ END OF BRAND FILTER CODE
wp_die(); // this is required to terminate immediately and return a proper response
}
?>