phpwordpressselectmenutaxonomy

How to echo taxonomy tags in the wp_dropdown_pages function?


For my Wordpress blog I have been experimenting with the wp_dropdown_pages function. With this code

 <form class="pagemenu" action="<?php bloginfo('url'); ?>" method="get">
    <?php
    $select = wp_dropdown_pages(
                        array(                            
                            'show_option_none' => 'Choisissez une autre liste de vocabulaire.',
                            'echo' => 0
                        )
                    );

    echo str_replace('<select ', '<select onchange="this.form.submit()" ', $select);
    ?>
</form>

I can render a dropdown menu with all the pages of the blog. But I was wondering if it was possible to echo the specific category of a page just in front of the page title. In my blog every page has one specific catgeory tag assigned, with a semantical function, and it would be good if this tag was rendered in the menu as well.

I have been researching this question for a few hours now, but could not find an answer.

Thanks for any suggestions or advice.


Solution

  • You can have category with following code :

    add_filter('list_pages', 'change_html', 10, 2);
    
    function change_html($title, $page){
        if(!is_admin()){
            //To get category
            $category = wp_get_object_terms( $page->ID, 'category' );
            $category_name = $category[0]->name;
    
            //To get tag
            $tag = wp_get_post_tags($page->ID);
            $tag_name = $tag[0]->name;
    
            return $title.' ('.$category_name.')';
        }
        return $title;
        //If you want Tag name, you can have it by following line
        //return $title.' ('.$tag_name.')';
    }