get child pages of parent ID and display custom image field with link to page in WordPress

纵然是瞬间 提交于 2020-03-15 07:53:30

问题


I need to get list of child pages, but i need to exclude the first child. I also need to display a custom image field called'page_icon' with url and page title for each. This will act as a sub menu whic will be displayed on each child page. Each menu item needs to have the current class attached to the a tag. i've tried a number of different methods and have had no luck as my skill level is beginner at best.

the parent ID is 1064 the custom field is called 'page_icon' which was set up using ACF

the current menu is hard coded into the editor and the html looks like this: -

<div id="attachment_1306" style="width: 160px" class="wp-caption alignleft"><a class="icon on" href="http://www.domain.co.uk/category/category/category/"><img class="wp-image-1306 size-thumbnail" title="Policy Manager" src="http://www.domain.co.uk/wp-content/uploads/2013/05/docs-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Policy Manager</p></div>

 <div id="attachment_1297" style="width: 160px" class="wp-caption alignleft"><a class="icon" href="http://www.domain.co.uk/category/category/category/"><img class="wp-image-1297 size-thumbnail" title="Risk Manager" src="http://www.domain.co.uk/wp-content/uploads/2013/05/risk-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Risk Manager</p></div>

 <div id="attachment_1307" style="width: 160px" class="wp-caption alignleft"><a class="icon" title="Controls Manager" href="http://www.domain.co.uk/governance-risk-and-category/category/category/"><img class="wp-image-1307 size-thumbnail" title="Controls Manager" src="http://www.domain.co.uk/wp-content/uploads/2013/05/controls-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Controls Manager</p></div>

 <div id="attachment_1301" style="width: 160px" class="wp-caption alignleft"><a class="icon" title="Incident Manager" href="http://www.domain.co.uk/category/category/category/"><img class="wp-image-1301 size-thumbnail" title="Incident Manager" src="http://www.domain.co.uk/wp-content/uploads/2013/05/incident-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Incident Manager</p></div>

 <div id="attachment_1289" style="width: 160px" class="wp-caption alignleft"><a class="icon" title="Workflow Manager" href="http://www.domain.co.uk/category/category/category/"><img class="wp-image-1289 size-thumbnail" title="Workflow Manager" src="http://www.corestream.co.uk/wp-content/uploads/2013/05/workflow-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Workflow Manager</p></div>

but this is not user friendly in terms of updating the page icons as you have to copy and paste this into each child page.

please someone help me?


回答1:


Create a file called solutions_menu.php in your theme's directory (/wp-content/themes/your-theme/solutions_menu.php). This file will contain the menu, and will be included in to your pages.

Edit:

I replaced the for-loop with a foreach-loop. Then I added another ACF field for when post type == page to control whether to show the page in the menu or not. It's a simple boolean (true/false) field with the name "appear_in_solutions_menu". I'm not sure this is the best way to solve it but it's working at least.

Here's a working example using the code below: http://japmag.net/24909031/?page_id=6

solutions_menu.php

<?php

# Parent ID
$parent_id = 1064;

# Arguments for the query
$args = array(
    'post_parent' => $parent_id,
    'post_type'   => 'page', 
    'posts_per_page' => -1,
    'post_status' => 'publish' 
    ); 

# The parent's children
$kids = get_children( $args );

# Current pages ID
$page_ID = get_the_ID();

# Start iterating from 1, to skip first child
foreach( $kids as $kid ) {

    # Set variables
    $id = $kid->ID;
    $url = get_permalink( $id );
    $page_icon_array = get_field( 'page_icon', $id );
    $page_icon = $page_icon_array[ 'url' ];
    $title = $kid->post_title;
    $show = get_field( 'appear_in_solutions_menu' , $id );

    # Make sure the page shoul be in the menu
    if( $show ) {

        # If the current page ID matches the ID of the child, then $current will contain " on", which will be used to apply the class "on" to the anchor-element
        $current = ( $page_ID == $id ) ? " on" : "";

        # Echo out the menu ?>

        <div style="width: 160px" class="wp-caption alignleft">

            <a class="icon<?php echo $current?>" href="<?php echo $url?>">

                <img class="size-thumbnail" title="<?php echo $title ?>" src="<?php echo $page_icon; ?>" alt="" width="150" height="150" />

            </a>

            <p class="wp-caption-text"><?php echo $title ?></p>

        </div>

    <?php 

    }

}

?>

Now replace the hard-coded menu in your different page files with

<?php get_template_part( 'solutions_menu' ); ?>

This will include the menu in to your pages, and should hopefully dynamically populate itself with the correct data. So if you need to make changes to the menu, just change solutions_menu.php and all the pages that include it will be affected. I hope I understood the problem correctly. I'm at work right now so I haven't been able to test the code yet.



来源:https://stackoverflow.com/questions/24909031/get-child-pages-of-parent-id-and-display-custom-image-field-with-link-to-page-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!