我在第页有这个代码。php,它调用my home的模板部分。在家里。php我使用query\\u posts而不是WP\\u query,我想将其更改为WP\\u query。
我试着改变它,但它已经不起作用了。
这是前面使用query\\u posts的代码,它正在工作:
$args = array(
\'post_type\' => \'post\',
"orderby" => "date",
"order" => "DESC",
"posts_per_page" => 24,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'terms\' => \'uncategorized\',
\'field\' => \'slug\'
),
array(
\'taxonomy\' => \'creatives-content-types\',
\'terms\' => \'newsfeed\',
\'field\' => \'slug\'
)
)
);
query_posts($args);
$count = 0;
$html_col_o = \'<div>\';
$html_col_t = \'<div>\';
$html_col_th = \'<div>\';
while ( have_posts() ) : the_post();
$count++;
$image = get_field(\'featured_image\', $post->ID);
$size = \'large\';
$image_url = $image[\'sizes\'][$size];
$description ="<h2>".get_the_title()."</h2>".get_field(\'description\',$post->ID);
if($count%3 == 1){
$html_col_o = $html_col_o. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
} else if($count%3 == 2){
$html_col_t = $html_col_t. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
} else if($count%3 == 0){
$html_col_th = $html_col_th. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
}
endwhile;
$html_col_o = $html_col_o.\'</div>\';
$html_col_t = $html_col_t.\'</div>\';
$html_col_th = $html_col_th.\'</div>\';
echo $html_col_o.$html_col_t.$html_col_th;
?>
这是使用WP\\u查询进行的转换,但不起作用:
$args = array(
\'post_type\' => \'post\',
"orderby" => "date",
"order" => "DESC",
"posts_per_page" => 24,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'terms\' => \'uncategorized\',
\'field\' => \'slug\'
),
array(
\'taxonomy\' => \'creatives-content-types\',
\'terms\' => \'newsfeed\',
\'field\' => \'slug\'
)
)
);
$custom_query = new WP_Query($args);
$count = 0;
$html_col_o = \'<div>\';
$html_col_t = \'<div>\';
$html_col_th = \'<div>\';
while ( $custom_query->have_posts() ) : $custom_query->the_post();
$count++;
$image = get_field(\'featured_image\', $custom_query->$post->ID);
$size = \'large\';
$image_url = $image[\'sizes\'][$size];
$description ="<h2>".get_the_title()."</h2>".get_field(\'description\',$custom_query->$post->ID);
if($count%3 == 1){
$html_col_o = $html_col_o. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
} else if($count%3 == 2){
$html_col_t = $html_col_t. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
} else if($count%3 == 0){
$html_col_th = $html_col_th. \'<img src="\'.$image_url.\'" id="img-\'.$count.\'" data-count="\'.$count.\'" data-description="\'.$description.\'"/>\';
}
endwhile;
$html_col_o = $html_col_o.\'</div>\';
$html_col_t = $html_col_t.\'</div>\';
$html_col_th = $html_col_th.\'</div>\';
echo $html_col_o.$html_col_t.$html_col_th;
当我尝试添加:echo“count:”时$自定义\\u查询->找到\\u帖子
它不会返回任何内容。
示例输出:(使用query\\u posts更正一个)
这就是使用wp\\u查询得到的结果
我该怎么办?我的代码有问题吗?请帮帮我。非常感谢。
最合适的回答,由SO网友:Pieter Goosen 整理而成
你没有理由用query_posts
而不是与WP_Query
. query_posts
使用WP_Query
, 因此,两者的作用应该相同。
你的代码真的很混乱,而且你使用了多种语法,这使得你的代码很难调试。这会导致隐藏明显的错误,并真的让你陷入不必要的疯狂追逐。此外,无需使用其默认值设置参数。
让我们将代码重写为更可读、更可靠的内容。
/**
* Set the query arguments we will use to get posts by
* Since PHP 5.4 we can use short array syntax, so we can use []
* instead of array(). You should be on PHP 5.6 already and should not
* be using any version prior to 5.6
*/
$args = [
\'posts_per_page\' => 24,
\'suppress_filters\' => true, // Do not let filters change the query
\'tax_query\' => [
[ // Removed the relation as AND is default value
\'taxonomy\' => \'category\',
\'terms\' => \'uncategorized\',
\'field\' => \'slug\'
],
[
\'taxonomy\' => \'creatives-content-types\',
\'terms\' => \'newsfeed\',
\'field\' => \'slug\'
]
]
];
$custom_query = new WP_Query($args);
if ( $custom_query->have_posts() ) { // Always first make sure you have posts to avoid bugs
$count = 0;
$html_col_o = \'<div>\';
$html_col_t = \'<div>\';
$html_col_th = \'<div>\';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
$count++;
$image = get_field( \'featured_image\', get_the_ID() ); // Use get_the_ID() for reliability
if ( $image ) { //Make sure we have an image to avoid bugs or unexpected output
$size = \'large\';
$image_url = $image[\'sizes\'][$size];
$description = \'<h2>\' . get_the_title() . \'</h2>\' . get_field( \'description\', get_the_ID() ); // Again, use get_the_ID()
if( $count%3 == 1 ) {
$html_col_o = $html_col_o . \'<img src="\' . $image_url . \'" id="img-\' . $count . \'" data-count="\' . $count . \'" data-description="\' . $description . \'"/>\';
} elseif( $count%3 == 2 ) {
$html_col_t = $html_col_t . \'<img src="\' . $image_url . \'" id="img-\' . $count . \'" data-count="\' . $count . \'" data-description="\' . $description . \'"/>\';
} elseif( $count%3 == 0 ) {
$html_col_th = $html_col_th . \'<img src="\' . $image_url . \'" id="img-\' . $count . \'" data-count="\' . $count . \'" data-description="\' . $description . \'"/>\';
}
}
}
$html_col_o = $html_col_o . \'</div>\';
$html_col_t = $html_col_t . \'</div>\';
$html_col_th = $html_col_th . \'</div>\';
echo $html_col_o . $html_col_t . $html_col_th;
wp_reset_postdata(); // VERY VERY IMPORTANT, restes the $post global back to the main query
}
你现在可以
echo $custom_query->request
要检查生成的SQL查询,也可以执行以下操作
var_dump( $custom_query )
检查完整的查询对象。您还应该打开debug并查找明显的bug。
但正如我所说,没有理由query_posts
工程和WP_Query
不