最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

title - Add anchor text to php

matteradmin10PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far