I am working with a hierarchy of pages and want to list the parent & children as a sidebar index (with current page bolded). It’s sort of working, but the PHP script I’ve come up with is using the main page heading to create the link text. These headings are very long for SEO purposes, so I’d like to replace them with the ACF ‘subtitle’ field that I’m already using. Here’s the project before I begin to explain:

https://newstart.staging.wpengine.com/treatment/mental-health/depression

In This Section shows the script-generated list first with the obnoxiously long titles. The shorter titles (beginning with “Mental Health”) were hard coded as placeholders, and that is what I would like the script to end up looking like. For example:

  • Depression is a Huge Risk Factor for Substance Abuse
  • (should be…)
  • Depression

How can I replace these titles with the subtitles? The get_field value in ACF is subtitle

Here is what the PHP looks like now:

<?php
//----  GET THE PAGE'S ANCESTORS
global $post;
$anc = get_post_ancestors( $post->ID );

        //----  IF $ANC HAS MORE THAN 1 VALUE
//----  THEN WE HAVE A 3RD LEVEL CHILD PAGE
//----  AND THUS WANT THE PARENT ID
        $top_id = $post->ID;
        if (count( $anc ) > 1 ){
            $top_id = $anc[0];
        }


        //----  TAKEN FROM WP CODEX
$ancestor_id = $top_id;
//VERY IMPORTANT, THE ORDER OF THIS ARRAY
//ENSURE THE SORTING MATCHES IN WP_LIST_PAGES BELOW
$descendants = get_pages(array(
            'child_of' => $ancestor_id,
             'sort_order' =>'ASC', 
             'sort_column'  => 'menu_order',
             ));
$incl = "";

foreach ($descendants as $page) {
    if (($page->post_parent == $ancestor_id) ||
    ($page->post_parent == $post->post_parent) ||
    ($page->post_parent == $post->ID))
    {
        $incl .= $page->ID . ",";
    }
}

//----  MAKE A FINAL ARRAY FOR USE 
//----  AS THE PREV//NEXT NAVIGATION
$pages_array = explode( ',', $incl );
array_pop($pages_array); //pop off last empty value
array_unshift( $pages_array, $ancestor_id); //add the top level page

// echo '<pre>';
// print_r($pages_array);
// echo '</pre>';
?>
<?php
        //FIND THE NEXT ITEM
        //IN THE PAGES ARRAY
        //AFTER THE CURRENT PAGE
        $curr_key = array_search($post->ID, $pages_array);
        $curr_key++;        
    ?>

    <?php if( $curr_key  < count($pages_array) ){ ?> 
        <?php 
            $id = $pages_array[$curr_key];
            $title = get_the_title( $id );  
            $post = get_post( $id );
        ?>
<div class="col-md-2 hidden-sm hidden-xs section-index">
<h6>In This Section</h6>
<ul>
<?php 
    wp_list_pages(array(
         "include" => $ancestor_id, 
         "link_before" => "", 
         "title_li" => "", 
     ));
    wp_list_pages(array(
        "child_of" => $ancestor_id,
         "include" => $incl, 
         "link_before" => "", 
         "title_li" => "", 
         'sort_order' =>'ASC', 
        'sort_column'  => 'menu_order',
     ));
?>
</ul>

I come from a design background so I am not good with PHP syntax yet. Most of this is inherited code that I only vaguely understand. Any help would be appreciated!

Read more here: Listing WordPress Pages – Replace heading with custom field text


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: