列出WordPress页面 - 用自定义字段文本替换标题

时间:2017-05-16 04:44:05

标签: php arrays wordpress

我正在使用页面层次结构,并希望列出父级&子项作为侧边栏索引(当前页面以粗体显示)。它有点工作,但我提出的PHP脚本是使用主页标题来创建链接文本。这些标题对于搜索引擎优化目的来说非常长,所以我想用ACF'字幕'来替换它们。我已经在使用的领域。在我开始解释之前,这是项目:

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

在本节中首先显示脚本生成的列表,其中包含令人讨厌的长标题。较短的标题(从&#34开始;心理健康")被硬编码为占位符,这就是我希望脚本最终看起来像。例如:

  • 抑郁症是药物滥用的巨大风险因素
  • (应该......)
  • 抑郁

如何用字幕替换这些标题? ACF中的get_field值为 subtitle

以下是PHP现在的样子:

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

我来自设计背景,所以我还不熟悉PHP语法。大多数这是我只是模糊地理解的继承代码。任何帮助将不胜感激!

0 个答案:

没有答案