簡體   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