自定义字段的WordPress的列表页面
问题描述:
我正在一个网站的菜单上列出所有页面有一个特定的菜单,并加载标题和自定义字段图像与“高级自定义字段” 我可以显示标题但是当我尝试显示所有的菜单上从当前页面会显示自定义字段图像的图像链接自定义字段的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>
感谢
答
使用get_post_meta,而不是get_field也许尝试。这听起来像get_field正在调用全球$ post-> ID而不是所需的$ page-> ID。
$attachment_id = get_post_meta($page->ID, 'front_billede', true);
答
在知道这是一个比较老的问题,但如果你想在这种情况下使用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')); ?>
得益于它完美地工作 – MyRevenge 2013-02-10 05:39:38