dongse7261 2018-07-05 10:55
浏览 74

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

So basically i have an Main Parent page with sub-category pages where each sub-category page has child pages and using the code provides, it works perfectly fine displaying each parent with its children.

I would like to know can have full control over the listing, because for only the parent tabs, i would like to add an image using ACF (advance custom fields) , meaning if that parent page in the CMS has a logo($tab_logo = get_field('parent_tab_logo');) add it to the listing with the parent name.

Code:

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

Current outcome: (example)

  • Main Parent PAGE

  • Parent-1 (child of Main Parent PAGE)

  • child1 (child of Parent-1)

  • child2 (child of Parent-1)

  • Parent-2 (child of Main Parent PAGE)

  • child1 (child of Parent-2)

  • child2 (child of Parent-2)

Expected outcome: (example)

  • Main Parent PAGE

  • Parent-1 (IMAGE from this page using acf = $tab_logo = get_field('parent_tab_logo');)

  • child1

  • child2

  • Parent-2 (IMAGE from this page using acf = $tab_logo = get_field('parent_tab_logo');)

  • child1

  • child2

  • 写回答

2条回答 默认 最新

  • dousa1630 2018-07-05 11:05
    关注

    This should be as simple as what the ACF docs say.

    You need to get the field when you check for a parent then display the field in the relevant section, passing the post id might also save you some headaches so it only targets your parent items.

    Hope this helps.

    <?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; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?