Wp obtenir une catégorie de publication personnalisée

Exemples de code

3
0

créez un type de publication personnalisé avec une catégorie dans les fonctions wordpress.php

function create_posttype() {
  register_post_type( 'wpll_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'products'),
    )
  );
}
add_action( 'init', 'create_posttype' );
2
0

wordpress obtenir le nom de la catégorie de publication

<?php
$categories = get_the_category();
 
if ( ! empty( $categories ) ) {
    echo esc_html( $categories[0]->name ); // Show first item of category  
}
?>
0
0

obtenir la catégorie principale de post wordpress

This is not a native wordpress feature, but a feature of Yoast SEO (see here).

You can check for primary status the following way:

<?php
$term_list = wp_get_post_terms($post->ID, 'category', ['fields' => 'all']);
foreach($term_list as $term) {
   if( get_post_meta($post->ID, '_yoast_wpseo_primary_category',true) == $term->term_id ) {
     // this is a primary category
   }
}
?>
If you are using custom taxonomies, use the meta_key

_yoast_wpseo_primary_CUSTOM_TAXONOMY
instead

Pages connexes

Pages d'exemples similaires

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................