web-dev-qa-db-fra.com

Le filtrage des types de publication personnalisés dans admin ne fonctionne pas

Bonjour, je me demande si quelqu'un peut m'aider à comprendre cela. Essentiellement, lorsque je clique sur mon type de message personnalisé "Portfolio" dans l’administrateur, je vois une liste de "Services" à côté des messages, mais lorsque je clique sur un pour filtrer (par exemple, http://localhost:8888/wp-admin/edit.php?post_type=portfolio&Service=web-design), rien ne se passe! Il répertorie toujours toutes les taxonomies.

Vous trouverez ci-dessous le code utilisé pour enregistrer le type de message personnalisé et la taxonomie (excuses pour la longueur, mais je ne peux pas déterminer ce qui ne va pas):

add_action('init','portfolio_register');

function portfolio_register(){

$labels=array(
'name'=> _x('Portfolio','posttype general name'),
'singular_name'=> _x('Project','posttype singular name'),
'add_new'=> _x('Add New Project','portfolio item'),
'add_new_item'=> __('Add New Project'),
'edit_item'=> __('Edit Project'),
'new_item'=> __('New Project'),
'view_item'=> __('View Project'),
'search_items'=> __('Search Portfolio'),
'not_found'=>  __('Nothing found'),
'not_found_in_trash'=> __('Nothing found in Trash'),
'parent_item_colon'=>'');

$args=array(
'labels'=>$labels,
'public'=>true,
'publicly_queryable'=>true,
'show_ui'=>true,
'show_in_menu'=>true,
'menu_position'=>5,
'query_var'=>true,
'menu_icon'=> get_stylesheet_directory_uri().'/images/mag.png',
'rewrite'=>true,'capability_type'=>'post',
'hierarchical'=>false,
'supports'=>array('title','editor','thumbnail','comments')); 

register_post_type('portfolio',$args);}

register_taxonomy("Service",array("portfolio"),array("hierarchical"=>true,"label"=>"Service","singular_label"=>"Service","rewrite"=>true));

Ensuite, pour afficher les colonnes dans admin:

add_filter( 'manage_edit-portfolio_columns', 'my_edit_portfolio_columns' ) ;

function my_edit_portfolio_columns( $columns ) {

    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => __( 'Project' ),
        'Service' => __( 'Services' ),
        'date' => __( 'Date' )
    );

    return $columns;
}

add_action( 'manage_portfolio_posts_custom_column', 'my_manage_portfolio_columns', 10, 2 );

function my_manage_portfolio_columns( $column, $post_id ) {
    global $post;

    switch( $column ) {


        /* If displaying the 'service' column. */
        case 'Service' :

            /* Get the genres for the post. */
            $terms = get_the_terms( $post_id, 'Service' );

            /* If terms were found. */
            if ( !empty( $terms ) ) {

                $out = array();

                /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                foreach ( $terms as $term ) {
                    $out[] = sprintf( '<a href="%s">%s</a>',
                        esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'Service' => $term->slug ), 'edit.php' ) ),
                        esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'Service', 'display' ) )
                    );
                }

                /* Join the terms, separating them with a comma. */
                echo join( ', ', $out );
            }

            /* If no terms were found, output a default message. */
            else {
                _e( 'No Services' );
            }

            break;

        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}
5
vonholmes

Fondamentalement, vous faites tout, sauf que la taxonomie dans l'URL devrait être en minuscule afin que vous puissiez simplement placer cette ligne:

esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'Service' => $term->slug ), 'edit.php' ) ),

avec ça:

esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'service' => $term->slug ), 'edit.php' ) ),

et ça devrait aller.

Et voici un bonus pour obtenir un menu déroulant comme filtre: enter image description here

// Adding a Taxonomy Filter to Admin List for a Custom Post Type
add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {

    // only display these taxonomy filters on desired custom post_type listings
    global $typenow;
    if ($typenow == 'portfolio') {

        // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
        $filters = array('Service');

        foreach ($filters as $tax_slug) {
            // retrieve the taxonomy object
            $tax_obj = get_taxonomy($tax_slug);
            $tax_name = $tax_obj->labels->name;

            // output html for taxonomy dropdown filter
            echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
            echo "<option value=''>Show All $tax_name</option>";
            generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
            echo "</select>";
        }
    }
}

function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
    $args = array('show_empty' => 1);
    if(!is_null($parent)) {
        $args = array('parent' => $parent);
    }
    $terms = get_terms($tax_slug,$args);
    $tab='';
    for($i=0;$i<$level;$i++){
        $tab.='--';
    }
    foreach ($terms as $term) {
        // output each select option line, check against the last $_GET to show the current option selected
        echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
        generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
    }

}
5
Bainternet