回显变量ID在WordPress中不起作用
我试图在回显变量ID在WordPress中不起作用,wordpress,function,variables,scope,Wordpress,Function,Variables,Scope,我试图在footer\u output()函数之外回显$ID,但我不确定如何让它工作。这是我的密码: add_action('wp_footer', 'footer_output', 10); function footer_output() { global $post; $args = array( 'post_type' => 'shoes', 'posts_per_page' => -1,
footer\u output()
函数之外回显$ID
,但我不确定如何让它工作。这是我的密码:
add_action('wp_footer', 'footer_output', 10);
function footer_output() {
global $post;
$args = array(
'post_type' => 'shoes',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => '==',
array(
'key' => 'women',
'compare' => 'EXISTS',
'value' => '1'
),
),
);
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post();
$the_ID = $post->ID;
$size = get_post_meta($the_ID, 'size', true);
$color = get_post_meta($the_ID, 'color', true);
// Do more stuff here...
endwhile;
wp_reset_postdata();
}
add_action('wp_head', 'header_output', 10);
function header_output() {
echo '<link rel="stylesheet" id="shoes-' . $the_ID . '" href="" media="all" />';
}
add_动作('wp_footer','footer_output',10);
函数页脚_输出(){
全球$员额;
$args=数组(
“post_type”=>“shoes”,
“每页帖子数”=>-1,
“元查询”=>数组(
“关系”=>“==”,
排列(
“关键”=>“女性”,
“比较”=>“存在”,
“值”=>“1”
),
),
);
$query=新的WP\u查询($args);
而($query->have_posts()):$query->the_post();
$theu ID=$post->ID;
$size=get\u post\u meta($ID,'size',true);
$color=get\u post\u meta($ID'color',true);
//在这里做更多的事情。。。
结束时;
wp_reset_postdata();
}
添加行动(“工作包头”,“标题输出”,10);
函数头_输出(){
回声';
}
但这当然行不通。有什么建议吗?你就快到了。您只需
返回$u ID从footer\u output()
中选择code>,并使用函数而不是$ID
var,如下所示:
add_action('wp_footer', 'footer_output', 10);
function footer_output() {
global $post;
$args = array(
'post_type' => 'shoes',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => '==',
array(
'key' => 'women',
'compare' => 'EXISTS',
'value' => '1'
),
),
);
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post();
$the_ID = $post->ID;
$size = get_post_meta($the_ID, 'size', true);
$color = get_post_meta($the_ID, 'color', true);
// Do more stuff here...
endwhile;
return $the_ID;
wp_reset_postdata();
}
add_action('wp_head', 'header_output', 10);
function header_output() {
echo '<link rel="stylesheet" id="shoes-' . footer_output() . '" href="" media="all" />';
}
add_动作('wp_footer','footer_output',10);
函数页脚_输出(){
全球$员额;
$args=数组(
“post_type”=>“shoes”,
“每页帖子数”=>-1,
“元查询”=>数组(
“关系”=>“==”,
排列(
“关键”=>“女性”,
“比较”=>“存在”,
“值”=>“1”
),
),
);
$query=新的WP\u查询($args);
而($query->have_posts()):$query->the_post();
$theu ID=$post->ID;
$size=get\u post\u meta($ID,'size',true);
$color=get\u post\u meta($ID'color',true);
//在这里做更多的事情。。。
结束时;
返回$u ID;
wp_reset_postdata();
}
添加行动(“工作包头”,“标题输出”,10);
函数头_输出(){
回声';
}