2013-02-09 21 views
0

我正在一個網站的菜單上列出所有頁面有一個特定的菜單,並加載標題和自定義字段圖像與「高級自定義字段」 我可以顯示標題但是當我嘗試顯示所有的菜單上從當前頁面會顯示自定義字段圖像的圖像鏈接自定義字段的WordPress的列表頁面

下面是一個例子「http://appelhat.dk/bordplader/」菜單,當鼠標觸及頁面的BUTTOM向上移動。

下面是代碼

<nav id="main-navigation"> 
     <ul> 
      <?php 
       $template = 'gallery.php'; 
       $Pages = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$template'"); 
       foreach($Pages as $Page) { 
        $exclude_Pages .= $Page . ','; 
       } 
       $pages = get_pages("include=$exclude_Pages"); 

      foreach ($pages as $page): 

       $title = $page->post_title 
       ?> 
       <li> 
        <a href="<?php echo get_page_link($page->ID); ?>"> 
         <span class="title"><?php echo $title ?></span> 
        <?php 
         $attachment_id = get_field('front_billede'); 
         $size = "thumbnail"; 
         $image = wp_get_attachment_image_src($attachment_id, $size); 
        ?> 
        <img src="<?php echo $image[0]; ?>" /> 
        </a> 
       </li> 
      <?php endforeach; ?> 
     </ul> 
    </nav> 

感謝

回答

1

使用get_post_meta,而不是get_field也許嘗試。這聽起來像get_field正在調用全球$ post-> ID而不是所需的$ page-> ID。

$attachment_id = get_post_meta($page->ID, 'front_billede', true);

或者,你可以創建一個輔助循環 - 使用WP_Queryget_posts,以確保所有的標籤都使用適當的ID。

+0

得益於它完美地工作 – MyRevenge 2013-02-10 05:39:38

0

在知道這是一個比較老的問題,但如果你想在這種情況下使用get_field那麼你可以簡單地使用重載的方法:

$attachment_id = get_field('front_billede', $page->ID); 

此外,如果你想創建這樣的自定義菜單在WordPress的話,我會推薦一個自定義的沃克。這裏是我以前使用的例子:

class WP_Image_Walker extends Walker_Page { 
    function start_lvl(&$output, $depth = 0, $args = array()) { 
     $indent = str_repeat("\t", $depth); 
     $output .= "\n$indent<ul class='children list-inline'>\n"; 
    } 

    function start_el(&$output, $page, $depth = 0, $args = array(), $current_page = 0) 
    { 
     if ($depth) { 
      $indent = str_repeat("\t", $depth); 
     } 
     else { 
      $indent = ''; 
     } 

     extract($args, EXTR_SKIP); 
     $css_class = array('page_item', 'page-item-'.$page->ID); 

     if (!empty($current_page)) { 
      $_current_page = get_page($current_page); 
      _get_post_ancestors($_current_page); 
      if (isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) 
       $css_class[] = 'current_page_ancestor'; 
       if ($page->ID == $current_page) 
        $css_class[] = 'current_page_item'; 
       elseif ($_current_page && $page->ID == $_current_page->post_parent) 
        $css_class[] = 'current_page_parent'; 
      } elseif ($page->ID == get_option('page_for_posts')) { 
       $css_class[] = 'current_page_parent'; 
      } 

      $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page, $depth, $args, $current_page)); 

      $image = get_field('image_field', $page->ID); 

      $output .= $indent . '<li class="' . $css_class . '">' . '<a href="' . get_permalink($page->ID) . '">'; 
      $output .= '<img src="' . $image['url'] . '" alt="' . apply_filters('the_title', $page->post_title, $page->ID) . '" />' . '</a>'; 

      if (!empty($show_date)) { 
       if ('modified' == $show_date) 
        $time = $page->post_modified; 
       else 
        $time = $page->post_date; 

      $output .= " " . mysql2date($date_format, $time); 
     } 
    } 
} 

然後用它在你這樣的頁面菜單:

<?php wp_page_menu(array('walker' => new WP_Image_Walker(), 'container' => 'false', 'menu_class' => 'nav-home')); ?>