Agregar categorías al tipo de publicación personalizada en permalink

20

Sé que la gente ha preguntado esto antes y ha ido tan lejos como para agregar el tipo de publicación personalizada y reescribir para permalink.

El problema es que tengo 340 categorías existentes que me gustaría seguir usando. Solía poder ver / categoría / subcategoría / nombre post

Ahora tengo el slug de customposttype / postname. La selección de la categoría ya no aparece en el enlace permanente ... No he cambiado la configuración del enlace permanente en admin a nada diferente.

¿Falta algo o debo agregarlo a este código?

function jcj_club_post_types() {
    register_post_type( 'jcj_club', array(
        'labels' => array(
            'name' => __( 'Jazz Clubs' ),
            'singular_name' => __( 'Jazz Club' ),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New Jazz Club' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Jazz Clubs' ),
            'new_item' => __( 'New Jazz Club' ),
            'view' => __( 'View Jazz Club' ),
            'view_item' => __( 'View Jazz Club' ),
            'search_items' => __( 'Search Jazz Clubs' ),
            'not_found' => __( 'No jazz clubs found' ),
            'not_found_in_trash' => __( 'No jazz clubs found in Trash' ),
            'parent' => __( 'Parent Jazz Club' ),
        ),
        'public' => true,
        'show_ui' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'menu_position' => 5,
        'query_var' => true,
        'supports' => array( 
            'title',
            'editor',
            'comments',
            'revisions',
            'trackbacks',
            'author',
            'excerpt',
            'thumbnail',
            'custom-fields',
        ),
        'rewrite' => array( 'slug' => 'jazz-clubs-in', 'with_front' => true ),
        'taxonomies' => array( 'category','post_tag'),
        'can_export' => true,
    )
);
    
pregunta hash 28.05.2012 - 17:33

4 respuestas

15

Hay dos puntos de ataque para cubrir cuando agrega reglas de reescritura de tipo de publicación personalizadas:

Reglas de reescritura

Esto sucede cuando las reglas de reescritura se generan en wp-includes/rewrite.php en WP_Rewrite::rewrite_rules() . WordPress le permite filtrar las reglas de reescritura para elementos específicos como publicaciones, páginas y varios tipos de archivo. Donde vea posttype_rewrite_rules , la parte posttype debe ser el nombre de su tipo de publicación personalizada. Alternativamente, puede usar el filtro post_rewrite_rules siempre que no borre las reglas estándar de publicación también.

A continuación, necesitamos la función para generar las reglas de reescritura:

// add our new permastruct to the rewrite rules
add_filter( 'posttype_rewrite_rules', 'add_permastruct' );

function add_permastruct( $rules ) {
    global $wp_rewrite;

    // set your desired permalink structure here
    $struct = '/%category%/%year%/%monthnum%/%postname%/';

    // use the WP rewrite rule generating function
    $rules = $wp_rewrite->generate_rewrite_rules(
        $struct,       // the permalink structure
        EP_PERMALINK,  // Endpoint mask: adds rewrite rules for single post endpoints like comments pages etc...
        false,         // Paged: add rewrite rules for paging eg. for archives (not needed here)
        true,          // Feed: add rewrite rules for feed endpoints
        true,          // For comments: whether the feed rules should be for post comments - on a singular page adds endpoints for comments feed
        false,         // Walk directories: whether to generate rules for each segment of the permastruct delimited by '/'. Always set to false otherwise custom rewrite rules will be too greedy, they appear at the top of the rules
        true           // Add custom endpoints
    );

    return $rules;
}

Lo principal a tener en cuenta aquí si decides jugar es el booleano 'Caminar directorios'. Genera reglas de reescritura para cada segmento de un permastruct y puede causar discrepancias de reglas de reescritura. Cuando se solicita una URL de WordPress, la matriz de reglas de reescritura se verifica de arriba a abajo. Tan pronto como se encuentre una coincidencia, se cargará lo que haya encontrado, por ejemplo, si su permastruct tiene una coincidencia codiciosa, por ejemplo. para los directorios /%category%/%postname%/ y walk está activado, generará reglas de reescritura tanto para /%category%/%postname%/ Y /%category%/ que coincidirán con cualquier cosa. Si eso sucede demasiado pronto, estás jodido.

Permalinks

Esta es la función que analiza los permalinks de tipo de publicación y convierte un permastruct (por ejemplo, '/% year% /% monthnum% /% postname% /') en una URL real.

La siguiente parte es un ejemplo simple de lo que idealmente sería una versión de la función get_permalink() encontrada en wp-includes/link-template.php . Los enlaces permanentes personalizados se generan mediante get_post_permalink() , que es una versión muy diluida de get_permalink() . get_post_permalink() se filtra por post_type_link , así que lo estamos utilizando para crear una estructura de estructura personalizada.

// parse the generated links
add_filter( 'post_type_link', 'custom_post_permalink', 10, 4 );

function custom_post_permalink( $permalink, $post, $leavename, $sample ) {

    // only do our stuff if we're using pretty permalinks
    // and if it's our target post type
    if ( $post->post_type == 'posttype' && get_option( 'permalink_structure' ) ) {

        // remember our desired permalink structure here
        // we need to generate the equivalent with real data
        // to match the rewrite rules set up from before

        $struct = '/%category%/%year%/%monthnum%/%postname%/';

        $rewritecodes = array(
            '%category%',
            '%year%',
            '%monthnum%',
            '%postname%'
        );

        // setup data
        $terms = get_the_terms($post->ID, 'category');
        $unixtime = strtotime( $post->post_date );

        // this code is from get_permalink()
        $category = '';
        if ( strpos($permalink, '%category%') !== false ) {
            $cats = get_the_category($post->ID);
            if ( $cats ) {
                usort($cats, '_usort_terms_by_ID'); // order by ID
                $category = $cats[0]->slug;
                if ( $parent = $cats[0]->parent )
                    $category = get_category_parents($parent, false, '/', true) . $category;
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if ( empty($category) ) {
                $default_category = get_category( get_option( 'default_category' ) );
                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
            }
        }

        $replacements = array(
            $category,
            date( 'Y', $unixtime ),
            date( 'm', $unixtime ),
            $post->post_name
        );

        // finish off the permalink
        $permalink = home_url( str_replace( $rewritecodes, $replacements, $struct ) );
        $permalink = user_trailingslashit($permalink, 'single');
    }

    return $permalink;
}

Como se mencionó, este es un caso muy simplificado para generar un conjunto de reglas y enlaces permanentes de reescritura personalizado, y no es particularmente flexible, pero debería ser suficiente para comenzar.

Hacer trampa

Escribí un complemento que te permite definir permastructs para cualquier tipo de publicación personalizada, pero al igual que puedes usar %category% en la estructura de permalink para las publicaciones, mi complemento admite %custom_taxonomy_name% para cualquier taxonomía personalizada que tengas, donde custom_taxonomy_name es el nombre de su taxonomía por ej. %club% .

Funcionará como cabría esperar con taxonomías jerárquicas / no jerárquicas.

enlace

    
respondido por el sanchothefat 20.12.2012 - 14:19
1

He encontrado una SOLUCIÓN !!!

(Después de una interminable investigación ... puedo tener TUS POSTUS A LA MEDIDA como enlaces permanentes como:
example.com/category/sub_category/my-post-name

aquí el código (en functions.php o plugin):

//===STEP 1 (affect only these CUSTOM POST TYPES)
$GLOBALS['my_post_typesss__MLSS'] = array('my_product1','....');



//===STEP 2  (create desired PERMALINKS)
add_filter('post_type_link',    'my_func88888', 6, 4 );
                        function my_func88888( $post_link, $post, $sdsd){
    if (!empty($post->post_type) && in_array($post->post_type, $GLOBALS['my_post_typesss']) ) {  
        $SLUGG = $post->post_name;
        $post_cats = get_the_category($id);     
        if (!empty($post_cats[0])){ $target_CAT= $post_cats[0];
            while(!empty($target_CAT->slug)){
                $SLUGG =  $target_CAT->slug .'/'.$SLUGG; 
                if  (!empty($target_CAT->parent)) {$target_CAT = get_term( $target_CAT->parent, 'category');}   else {break;}
            }
            $post_link= get_option('home').'/'. urldecode($SLUGG);
        }
    }
    return  $post_link;
}


// STEP 3  (by default, while accessing:  "EXAMPLE.COM/category/postname"     WP thinks, that a standard post is requested. So, we are adding CUSTOM POST TYPE into that query.
add_action('pre_get_posts', 'my_func4444',  12); 
                    function my_func4444($q){     
    if ($q->is_main_query() && !is_admin() && $q->is_single){
        $q->set( 'post_type',  array_merge(array('post'), $GLOBALS['my_post_typesss'] )   );
    }
    return $q;
}
    
respondido por el T.Todua 26.07.2015 - 21:07
1

Tengo la solución!

Para tener los enlaces permanentes jerárquicos para el tipo de publicación personalizada, instale el tipo de publicación personalizada Permalinks ( enlace ).

Actualizar el tipo de publicación registrada. Tengo el nombre del tipo de publicación como centro de ayuda

function help_centre_post_type(){
    register_post_type('helpcentre', array( 
        'labels'            =>  array(
            'name'          =>      __('Help Center'),
            'singular_name' =>      __('Help Center'),
            'all_items'     =>      __('View Posts'),
            'add_new'       =>      __('New Post'),
            'add_new_item'  =>      __('New Help Center'),
            'edit_item'     =>      __('Edit Help Center'),
            'view_item'     =>      __('View Help Center'),
            'search_items'  =>      __('Search Help Center'),
            'no_found'      =>      __('No Help Center Post Found'),
            'not_found_in_trash' => __('No Help Center Post in Trash')
                                ),
        'public'            =>  true,
        'publicly_queryable'=>  true,
        'show_ui'           =>  true, 
        'query_var'         =>  true,
        'show_in_nav_menus' =>  false,
        'capability_type'   =>  'page',
        'hierarchical'      =>  true,
        'rewrite'=> [
            'slug' => 'help-center',
            "with_front" => false
        ],
        "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
        'menu_position'     =>  21,
        'supports'          =>  array('title','editor', 'thumbnail'),
        'has_archive'       =>  true
    ));
    flush_rewrite_rules();
}
add_action('init', 'help_centre_post_type');

Y aquí está registrada la taxonomía

function themes_taxonomy() {  
    register_taxonomy(  
        'help_centre_category',  
        'helpcentre',        
        array(
            'label' => __( 'Categories' ),
            'rewrite'=> [
                'slug' => 'help-center',
                "with_front" => false
            ],
            "cptp_permalink_structure" => "/%help_centre_category%/",
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'query_var' => true
        ) 
    );  
}  
add_action( 'init', 'themes_taxonomy');

Esta línea hace que tu enlace permanente funcione

"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",

puedes eliminar %post_id% y puedes mantener /%help_centre_category%/%postname%/"

No te olvides de vaciar los enlaces permanentes del panel de control.

    
respondido por el Varsha Dhadge 20.07.2017 - 14:54
-2

Tienes varios errores con tu código. Limpié tu código existente:

<?php
function jcj_club_post_types() {
  $labels = array(
    'name' => __( 'Jazz Clubs' ),
    'singular_name' => __( 'Jazz Club' ),
    'add_new' => __( 'Add New' ),
    'add_new_item' => __( 'Add New Jazz Club' ),
    'edit' => __( 'Edit' ),
    'edit_item' => __( 'Edit Jazz Clubs' ),
    'new_item' => __( 'New Jazz Club' ),
    'view' => __( 'View Jazz Club' ),
    'view_item' => __( 'View Jazz Club' ),
    'search_items' => __( 'Search Jazz Clubs' ),
    'not_found' => __( 'No jazz clubs found' ),
    'not_found_in_trash' => __( 'No jazz clubs found in Trash' ),
    'parent' => __( 'Parent Jazz Club' ),
    );
  $args = array(
    'public' => true,
    'show_ui' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'menu_position' => 5,
    'query_var' => true,
    'supports' => array( 'title','editor','comments','revisions','trackbacks','author','excerpt','thumbnail','custom-fields' ),
    'rewrite' => array( 'slug' => 'jazz-clubs-in', 'with_front' => true ),
    'has_archive' => true
    );
  register_post_type( 'jcj_club', $args );
  }
add_action( 'init','jcj_club_post_types' );
?>

Reemplace su código con el código anterior y vea si funciona. Responda si tiene más preguntas e intentaré ayudar.

EDITAR:

Me di cuenta de que omití 'has_archive' => true .

    
respondido por el Jeremy Jared 30.05.2012 - 04:30

Lea otras preguntas en las etiquetas