gpt4 book ai didi

php - wordpress menu-walker 中的 ACF 对象

转载 作者:可可西里 更新时间:2023-10-31 23:47:23 26 4
gpt4 key购买 nike

我有一个 WP 站点,我希望每个 menu-items 都有一个 sub-menu,一个 li-item每个 the_sub_field('info_block_header')。所以我正在尝试创建一个内部有 ACF 循环的助行器,但是我现在不知道如何正确实现循环。这是我得到的,但结果很糟糕:

侧边栏.php:

<aside>
<nav id="mainNav">

<?php
$dropdown = new dropdown_walker;

$mainNav = array(
'theme_location' => '',
'menu' => 'main-menu',
'container' => 'ul',
'container_class' => 'topmenu-{topmenu slug}-container',
'container_id' => 'topmenu',
'menu_class' => 'topmenu',
'menu_id' => 'topmenu-{topmenu slug}[-{increment}]',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '<h4>',
'link_after' => '</h4>',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => $dropdown
);

wp_nav_menu( $mainNav );


?>

<?php get_search_form(); ?>
</aside>

页面.php

<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if(get_field('info_block')): ?>
<?php while(has_sub_field('info_block')): ?>
<div class="box masonCh boxText col2 clearfix">
<h2><?php the_sub_field('info_block_header')?></h2>
<div>
<?php the_sub_field('info_block_txt')?>
</div>

</div>
<?php if ( have_rows('info_block_img')):
while (have_rows ('info_block_img')) : the_row();?>
<div class="box masonCh boxImg col2 clearfix">
<div class="boxCrop">
<img src="<?php the_sub_field('info_block_img_file') ?>">
</div>
</div>

下拉菜单.php

<?php 
class dropdown_walker extends Walker {

var $tree_type = array( 'post_type', 'taxonomy', 'custom' );

var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

$output .= $indent . '<li' . $id . $value . $class_names .'>';

$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';


$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );

$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';

$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= $args->after;
$item_output .= '</a>';


$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

//Here’s where I’m trying to put my submenu:

$output .= '<ul id="about" class="dropDown">'. if(get_field('info_block')):
while(has_sub_field('info_block')): .'
<li><a href="#">'. get_sub_field('info_block_header') .'</a></li>'. endwhile; endif; .
</ul>';

} function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}

} // Walker_Nav_Menu
?>

最佳答案

你要传ID,关联:你可以拿到$item->ID,就可以知道这个item的类型 print_r($item->type); (分类法、页面等)。

然后:

$output .= '<ul id="about" class="dropDown">'. if(get_field('info_block', $id)):
while(has_sub_field('info_block', $id)): .'
<li><a href="#">'. get_sub_field('info_block_header', $id) .'</a></li>'. endwhile; endif; .
</ul>';

关于php - wordpress menu-walker 中的 ACF 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com