带有自定义字段的Wordpress列表页面

时间:2013-02-09 15:02:14

标签: wordpress wordpress-theming

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

这是一个示例“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>

由于

2 个答案:

答案 0 :(得分:1)

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

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

或者,您可以使用WP_Queryget_posts创建辅助循环,以确保所有代码都使用相应的ID。

答案 1 :(得分:0)

知道这是一个相对古老的问题,但是如果你想在这个上下文中使用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') ); ?>