@lucagrandicelli here is the code:
Class CP_Testimonial {
/**
* Initialize all the things
*/
public static $post_name = 'testimonial';
public static $tag_taxonomy = 'testimonial_tag';
function __construct() {
// Actions
add_action( 'init', array( $this, 'register_cpt' ) );
add_action( 'after_setup_theme', array( $this, 'register_ct_tags' ), 1 );
}
/**
* Register the custom post type
*/
public function register_cpt() {
$labels = array(
'all_items' => __( 'All Testimonials', 'ediloc' ),
'name' => __( 'Testimonials', 'ediloc' ),
'singular_name' => __( 'Testimonial', 'ediloc' ),
'add_new' => __( 'Add New', 'ediloc' ),
'add_new_item' => __( 'Add New Testimonial', 'ediloc' ),
'edit_item' => __( 'Edit Testimonial', 'ediloc' ),
'update_item' => __( 'Update Testimonial', 'ediloc' ),
'new_item' => __( 'New Testimonial', 'ediloc' ),
'view_item' => __( 'View Testimonial', 'ediloc' ),
'search_items' => __( 'Search Testimonials', 'ediloc' ),
'not_found' => __( 'No Testimonials found', 'ediloc' ),
'not_found_in_trash' => __( 'No Testimonials found in Trash', 'ediloc' ),
'parent_item_colon' => __( 'Parent Testimonial:', 'ediloc' ),
'menu_name' => __( 'Testimonials', 'ediloc' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'Testimonials for the website.', 'ediloc' ),
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
'public' => false,
'show_ui' => true,
'show_in_rest' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => false,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'can_export' => true,
'show_in_admin_bar' => true,
//'rewrite' => array( 'slug' => 'testimonial', 'with_front' => false ),
'menu_position' => 20,
'menu_icon' => 'dashicons-format-quote',
//'template' => array( array( 'core/quote', array( 'className' => 'is-style-large' ) ) ),
//'template_lock' => 'all',
);
register_post_type( self::$post_name , $args );
}
//category type taxonomy tags
public function register_ct_tags() {
$labels = array(
'name' => __( 'Tags', 'ediloc' ),
'singular_name' => __( 'Tag', 'ediloc' ),
'search_items' => __( 'Search Tag', 'ediloc' ),
'all_items' => __( 'All Tags', 'ediloc' ),
'parent_item' => __( 'Parent Tag', 'ediloc' ),
'parent_item_colon' => __( 'Parent Tag:', 'ediloc' ),
'edit_item' => __( 'Edit Tag', 'ediloc' ),
'update_item' => __( 'Update Tag', 'ediloc' ),
'add_new_item' => __( 'Add New Tag', 'ediloc' ),
'new_item_name' => __( 'New Tag Name', 'ediloc' ),
'menu_name' => __( 'Tags', 'ediloc' ),
);
register_taxonomy(self::$tag_taxonomy, array(self::$post_name), array(
'public' => true,
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => self::$tag_taxonomy ),
));
}
public static function get_random($limit, $term_ids) {
$args = array(
'post_type' => self::$post_name,
'tax_query' => array(
array(
'taxonomy' => self::$tag_taxonomy,
'field' => 'id',
'terms' => $term_ids
)
),
'orderby' => 'rand',
'posts_per_page' => $limit,
);
$testimonials = new WP_Query( $args );
return $testimonials;
}
public static function get_tags() {
$tags = [];
$terms = get_terms( [
'taxonomy' => self::$tag_taxonomy,
'hide_empty' => true,
]);
if (!empty($terms)) {
foreach($terms as $term) {
$tags[$term->term_id] = $term->name;
}
}
return $tags;
}
}
new CP_Testimonial();
-
This reply was modified 7 years, 5 months ago by
speedwheel.
-
This reply was modified 7 years, 5 months ago by
speedwheel.
-
This reply was modified 7 years, 5 months ago by
speedwheel.