Custom post type plugin evenimente

De curand am realizat pentru un prieten un mic plugin custom post type evenimente. As dorii sa pun la dispozitie codurile folosite poate ajuta pe cineva la vreun proiect. Se dorea afisarea ultimelelor 4 evenimente dinamic pe prima pagina.

Primele 2 functii din plugin sunt pentru inregistrarea in WordPress a unui post type numit evenimente.

// Register Custom Post Type
function custom_post_type_evenimente() {

  $labels = array(
    'name'                  => _x( 'Evenimente', 'Post Type General Name', 'ventilatoare' ),
    'singular_name'         => _x( 'Eveniment', 'Post Type Singular Name', 'ventilatoare' ),
    'menu_name'             => __( 'Evenimente', 'ventilatoare' ),
    'name_admin_bar'        => __( 'Evenimente', 'ventilatoare' ),
    'archives'              => __( 'Item Archives', 'ventilatoare' ),
    'parent_item_colon'     => __( 'Parent Item:', 'ventilatoare' ),
    'all_items'             => __( 'Toate', 'ventilatoare' ),
    'add_new_item'          => __( 'Adauga eveniment nou', 'ventilatoare' ),
    'add_new'               => __( 'Adauga nou', 'ventilatoare' ),
    'new_item'              => __( 'New Item', 'ventilatoare' ),
    'edit_item'             => __( 'Editeaza', 'ventilatoare' ),
    'update_item'           => __( 'Update', 'ventilatoare' ),
    'view_item'             => __( 'Vezi evenimentul', 'ventilatoare' ),
    'search_items'          => __( 'Cauta eveniment', 'ventilatoare' ),
    'not_found'             => __( 'Not found', 'ventilatoare' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'ventilatoare' ),
    'featured_image'        => __( 'Featured Image', 'ventilatoare' ),
    'set_featured_image'    => __( 'Set featured image', 'ventilatoare' ),
    'remove_featured_image' => __( 'Remove featured image', 'ventilatoare' ),
    'use_featured_image'    => __( 'Use as featured image', 'ventilatoare' ),
    'insert_into_item'      => __( 'Insert into item', 'ventilatoare' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'ventilatoare' ),
    'items_list'            => __( 'Items list', 'ventilatoare' ),
    'items_list_navigation' => __( 'Items list navigation', 'ventilatoare' ),
    'filter_items_list'     => __( 'Filter items list', 'ventilatoare' ),
  );
  $args = array(
    'label'                 => __( 'Eveniment', 'ventilatoare' ),
    'description'           => __( 'Evenimente', 'ventilatoare' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'thumbnail', ),
 // 'taxonomies'            => array( 'category', 'post_tag' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,    
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'post',
  );
  register_post_type( 'eveniment', $args );

}
add_action( 'init', 'custom_post_type_evenimente', 0 );



function create_eveniment_taxonomies() {
    $labels = array(
        'name'              => _x( 'Categorii', 'taxonomy general name' ),
        'singular_name'     => _x( 'Categorie', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'Toate Categoriile' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'categories' ),
    );

    register_taxonomy( 'eveniment_categories', array( 'eveniment' ), $args );
}
add_action( 'init', 'create_eveniment_taxonomies', 0 );

A doua functie din plugin este de a crea un shortcode pentru folosirea functie in editor.

function eveniment_shortcode( $atts, $content = null ) {
    
    extract(shortcode_atts(array(
        "limit" => '',
        'category' => '',
        'posts' => -1,
    ), $atts));


    // Define limit
    if( $limit ) { 
        $posts_per_page = $limit; 
    } else {
        $posts_per_page = '-1';
    }

    
    ob_start();

    // Create the Query
    $post_type      = 'eveniment';
    $orderby        = 'post_date';
    $order          = 'DESC';
    $posts_per_page = 4;
                
    $query = new WP_Query( 
         array ( 
                'post_type'      => $post_type,
                'posts_per_page' => $posts_per_page,
                'orderby'        => $orderby, 
                'order'          => $order,
                'category__in'   => '',
                'no_found_rows'  => 1
                ) 

        );
    
    //Get post type count
    $post_count = $query->post_count;
    
    
    // Displays Custom post info
    if( $post_count > 0) : ?>
    
    <div class="articole-grid">

     <?php
        // Loop
        while ($query->have_posts()) : $query->the_post(); ?>

       <div class="item">
        
          <?php if ( has_post_thumbnail() ) : ?>
           <div class="has_post_thumbnail">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
              <?php the_post_thumbnail('articole'); ?>
            </a>
         </div>
        <?php endif; ?>
        
        <span class="entry-date"><?php echo get_the_date(); ?></span>
        
         <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
           <h4 class="titlu_ar"><?php the_title(); ?></h4>
         </a>
    
       </div>

      <?php  
      endwhile;
      endif;
    
    // Reset query to prevent conflicts
    wp_reset_query();
    ?>
     
    </div>
    <?php
    
    return ob_get_clean();
}
add_shortcode("evenimente", "eveniment_shortcode");

Prin folosirea acestui shortcode [evenimente] veti afisa cele mai recente 4 evenimente pe prima pagina.

Toate codurile complete le veti gasii in arhiva de mai jos.

Evenimente plugin (658 downloads)

 

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Alte articole Populare