title - Add anchor text to php

admin2025-06-03  4

I am using this code to display a list of child pages of a parent:

<?php
global $post; // Setup the global variable $post
if ( is_page() && $post->post_parent ) {
    // Make sure we are on a page and that the page is a parent.
    $kiddies = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );  
} else {
    $kiddies = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
}

if ( $kiddies ) {
    echo '<ul class="secondary">';
        echo $kiddies;
    echo '</ul>';
}

I want to be able to customise the anchor text of the link. e.g. if the child page name is "blue" I want to append "widget" to the end to make the anchor "blue widget".

Can anyone help?

Thanks

I am using this code to display a list of child pages of a parent:

<?php
global $post; // Setup the global variable $post
if ( is_page() && $post->post_parent ) {
    // Make sure we are on a page and that the page is a parent.
    $kiddies = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );  
} else {
    $kiddies = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
}

if ( $kiddies ) {
    echo '<ul class="secondary">';
        echo $kiddies;
    echo '</ul>';
}

I want to be able to customise the anchor text of the link. e.g. if the child page name is "blue" I want to append "widget" to the end to make the anchor "blue widget".

Can anyone help?

Thanks

Share Improve this question edited May 15, 2016 at 19:10 Sumit 4,8542 gold badges29 silver badges36 bronze badges asked May 15, 2016 at 18:51 jecshjecsh 111 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 2

You can use the_title filter. Just remember to remove it after wp_list_pages() otherwise it will effect everywhere on the site like in menu, main title, search results etc.

Example:-

add_filter('the_title', 'my_custom_title');
wp_list_pages();
remove_filter('the_title', 'my_custom_title');

function my_custom_title($current_title) {
    if ($current_title == 'blue') {
        $current_title = $current_title . ' widget';
    }

    return $current_title;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748946954a315060.html

最新回复(0)