【问题标题】:Wordpress list pages with custom field带有自定义字段的 Wordpress 列表页面
【发布时间】:2013-02-09 15:02:14
【问题描述】:

我正在一个带有菜单的网站上工作,该菜单列出了所有具有特定菜单的页面并加载标题和使用“高级自定义字段”生成的自定义字段图像 我可以显示标题,但是当我尝试显示图像时,它会在所有菜单链接上显示当前页面中的自定义字段图像

这是一个示例“http://appelhat.dk/bordplader/”,当鼠标触摸页面的按钮时,菜单会向上移动。

这里是代码

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

谢谢

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    也许尝试使用get_post_meta 而不是get_field。听起来 get_field 正在调用全局 $post->ID 而不是所需的 $page->ID。

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

    或者,您可以创建辅助循环 - 使用 WP_Queryget_posts 以确保所有标签都使用适当的 ID。

    【讨论】:

      【解决方案2】:

      知道这是一个比较老的问题,但是如果你想在这种情况下使用 get_field 那么你可以简单地使用重载方法:

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

      此外,如果您想在 Wordpress 中创建这样的自定义菜单,那么我建议您使用自定义 Walker。这是我之前使用的一个示例:

      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') ); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 2014-01-09
        相关资源
        最近更新 更多