代码之家  ›  专栏  ›  技术社区  ›  Ylama

Wordpress页列表添加控件到列表项

  •  0
  • Ylama  · 技术社区  · 6 年前

    $tab_logo = get_field('parent_tab_logo'); )使用父名称将其添加到列表中。

    代码:

    <?php
    
    // find parent of current page
    if ($post->post_parent) {
    
        $ancestors = get_post_ancestors($post->ID);
        $parent = $ancestors[count($ancestors) - 1];
    
        //Display Parent post Title
        $parent_post_id = $parent;
        $parent_post = get_post($parent_post_id);
        $parent_post_title = $parent_post->post_title;
    
    } else {
        $parent = $post->ID;
    }
    
    $children = wp_list_pages("sort_order=asc&title_li=&child_of=" . $parent . "&echo=0");
    
    if ($children) { ?>
       <div id="side" class="col-lg-4 col-md-4 col-sm-12">
    
            <h4><?php echo $parent_post_title; ?></h4>
                  <div class="row">
                    <div class="col-lg-10 col-md-12 col-sm-12">
                      <ul id="side-navi" class="list-unstyled mb-0 ">
    
                          <?php echo $children; ?>
    
                      </ul>
                    </div>
                  </div>
    
        </div>
    
    <?php } ?> 
    

    • 主父页

    • child1(父项1的子项)

    • child2(父项1的子项)

    • 子女1(父母2的子女)

    预期结果: (示例)

    • 主父页

    • Parent-1(此页面中的图像使用acf= $tab_logo=get_field('parent_tab_logo'); )

    • 孩子1

    • 孩子2

    • $tab_logo=get_field('parent_tab_logo'); )

    • 孩子1

    • 孩子2

    2 回复  |  直到 6 年前
        1
  •  0
  •   lky    6 年前

    您应该能够执行如下操作,将父级post id传递到get_field()函数中。根据您在ACF中设置映像的方式,您可能需要更新拉出映像的方式。

    <?php
    
    // find parent of current page
    if ($post->post_parent) {
    
      $ancestors = get_post_ancestors($post->ID);
      $parent = $ancestors[count($ancestors) - 1];
    
      //Display Parent post Title
      $parent_post_id = $parent;
      $parent_post = get_post($parent_post_id);
      $parent_post_title = $parent_post->post_title;
      $parent_image = get_field('parent_tab_logo', $parent_post_id);
    
    } else {
      $parent = $post->ID;
    }
    
    $children = wp_list_pages("sort_order=asc&title_li=&child_of=" . $parent . "&echo=0");
    
    if ($children) { 
      ?>
      <div id="side" class="col-lg-4 col-md-4 col-sm-12">
    
        <h4><?php echo $parent_post_title; ?></h4>
        <?php
          if( !empty($parent_image) ){
          ?>
          <img src="<?php echo $parent_image['url']; ?>" alt="<?php echo $parent_image['alt']; ?>" />
          <?php
          } 
        ?>
    
    
        <div class="row">
          <div class="col-lg-10 col-md-12 col-sm-12">
            <ul id="side-navi" class="list-unstyled mb-0 ">
              <?php echo $children; ?>
            </ul>
          </div>
        </div>
      </div>
      <?php 
    } 
    ?> 
    
        2
  •  0
  •   lky    6 年前

    希望这有帮助。

    <?php 
    
    $parent_image = get_field('image', $post_id);
    
    if( !empty($parent_image) ): ?>
    
    <img src="<?php echo $parent_image['url']; ?>" alt="<?php echo $parent_image['alt']; ?>" />
    
    <?php endif; ?>