在header.php中使用WordPress ACF中继器



在ACF中,我有一个名为"slider"的中继器,带有单选按钮字段。这出现在网站的主页上。

我想在标题.php中输出单选按钮字段。这是我尝试过的:

<?php
if( have_rows('slider',$post->ID) ):
while ( have_rows('slider',$post->ID) ) : the_row();
if(get_sub_field('logo_type',$post->ID) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>

即使我尝试var_dump(get_sub_field('logo_type',$post->ID));,这也是空的

我也试过:

<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type',$postid) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>

我在这里做错了什么?

您是否尝试过没有此$post->ID

<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>

我不知道这是否会解决您的问题get_sub_field但第二个参数不应该是帖子 id,而是格式值。因此,在这种情况下,您将将其留空。

<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>

我还建议调试您从$postid获得的 ID。

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;
?>

我认为您需要将自定义字段与菜单一起添加。 即创建带有标题的字段组,分配菜单等于您的标题菜单名称,然后使用以下代码调用该字段

$menu = wp_get_nav_menu_object('menuid');//replace with your menu id.
the_field('logo_type',$menu);

您可以在任何地方访问它,您可以在 Apperance->Menus->Menu-name->acf-field-name 中看到此字段

最新更新