繁体   English   中英

在header.php中使用WordPress ACF转发器

[英]Using WordPress ACF repeater in header.php

在ACF中,我有一个带有单选按钮字段的中继器“ slider”。 这将显示在网站的主页上。

我想在header.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中看到此字段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM