Chào mừng đến với Diễn Đàn Tin Học VN! Hãy chia sẽ những gì bạn biết, và đưa ra thắc mắc của bạn để được giải đáp.

Xem chủ đề cũ hơnGo downXem chủ đề mới hơn
phoenix51706
phoenix51706
Admin
Tổng số bài gửi : 171
Join date : 30/07/2010
https://diendantinhocvn.forumvi.com

Tạo custom post và custom field cho wordpress Empty Tạo custom post và custom field cho wordpress

28/12/21, 06:23 am
Code:
<?php

//add_meta_box( $id, $title, $callback, $screen, $context, $priority, $callback_args)

/**
 * Register meta boxes.
 */
function phoenix_register_meta_boxes() {
    add_meta_box( 'phoenix-1', __( 'Infomation Field', 'phoenix' ), 'phoenix_display_callback', 'custom_post_type' );
}
add_action( 'add_meta_boxes', 'phoenix_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function phoenix_display_callback( $post ) {
    //echo "Hello Custom Field";
    ?>

    <div class="phoenix_box">
        <style scoped>
            .phoenix_box{
                display: grid;
                grid-template-columns: max-content 1fr;
                grid-row-gap: 10px;
                grid-column-gap: 20px;
            }
            .phoenix_field{
                display: contents;
            }
        </style>
        <!-- <p class="meta-options phoenix_field">
            <label for="phoenix_address">Address: </label>
            <input id="phoenix_address" type="text" name="phoenix_address" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_address', true ) ); ?>">
        </p>
        <p class="meta-options phoenix_field">
            <label for="phoenix_contact">Contact: </label>
            <input id="phoenix_contact" type="text"  name="phoenix_contact" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_contact', true ) ); ?>">
        </p>
        <p class="meta-options phoenix_field">
            <label for="phoenix_website">Website: </label>
            <input id="phoenix_website" type="url"  name="phoenix_website" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_website', true ) ); ?>">
        </p>
        <p class="meta-options phoenix_field">
            <label for="phoenix_facebook">Facebook: </label>
            <input id="phoenix_facebook" type="text"  name="phoenix_facebook" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_facebook', true ) ); ?>">
        </p> -->
        <p class="meta-options phoenix_field">
            <label for="phoenix_description">Description: </label>
            <input id="phoenix_description" type="text"  name="phoenix_description" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_description', true ) ); ?>">
        </p>
        <p class="meta-options phoenix_field">
            <label for="phoenix_link_1">Link 1: </label>
            <input id="phoenix_link_1" type="text"  name="phoenix_link_1" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_link_1', true ) ); ?>">
        </p>
        <p class="meta-options phoenix_field">
            <label for="phoenix_link_2">Link 2: </label>
            <input id="phoenix_link_2" type="text"  name="phoenix_link_2" value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'phoenix_link_2', true ) ); ?>">
        </p>
       
    </div>
   
   
<?php
}

function phoenix_save_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }
    $fields = [
        //'phoenix_address',
        //'phoenix_contact',
        //'phoenix_website',
      // 'phoenix_facebook',
        'phoenix_description',
        'phoenix_link_1',
        'phoenix_link_2',
    ];
    foreach ( $fields as $field ) {
        if ( array_key_exists( $field, $_POST ) ) {
            update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
        }
    }
}
add_action( 'save_post', 'phoenix_save_meta_box' );


// Register Custom Post Type
function custom_post_type() {

   $labels = array(
      'name'                  => _x( 'Post Types', 'Post Type General Name', 'enforevo' ),
      'singular_name'        => _x( 'Post Type', 'Post Type Singular Name', 'enforevo' ),
      'menu_name'            => __( 'Custom Post', 'enforevo' ),
      'name_admin_bar'        => __( 'Post Type', 'enforevo' ),
      'archives'              => __( 'Item Archives', 'enforevo' ),
      'attributes'            => __( 'Item Attributes', 'enforevo' ),
      'parent_item_colon'    => __( 'Parent Item:', 'enforevo' ),
      'all_items'            => __( 'All Items', 'enforevo' ),
      'add_new_item'          => __( 'Add New Item', 'enforevo' ),
      'add_new'              => __( 'Add New', 'enforevo' ),
      'new_item'              => __( 'New Item', 'enforevo' ),
      'edit_item'            => __( 'Edit Item', 'enforevo' ),
      'update_item'          => __( 'Update Item', 'enforevo' ),
      'view_item'            => __( 'View Item', 'enforevo' ),
      'view_items'            => __( 'View Items', 'enforevo' ),
      'search_items'          => __( 'Search Item', 'enforevo' ),
      'not_found'            => __( 'Not found', 'enforevo' ),
      'not_found_in_trash'    => __( 'Not found in Trash', 'enforevo' ),
      'featured_image'        => __( 'Featured Image', 'enforevo' ),
      'set_featured_image'    => __( 'Set featured image', 'enforevo' ),
      'remove_featured_image' => __( 'Remove featured image', 'enforevo' ),
      'use_featured_image'    => __( 'Use as featured image', 'enforevo' ),
      'insert_into_item'      => __( 'Insert into item', 'enforevo' ),
      'uploaded_to_this_item' => __( 'Uploaded to this item', 'enforevo' ),
      'items_list'            => __( 'Items list', 'enforevo' ),
      'items_list_navigation' => __( 'Items list navigation', 'enforevo' ),
      'filter_items_list'    => __( 'Filter items list', 'enforevo' ),
   );
   $args = array(
      'label'                => __( 'Post Type', 'enforevo' ),
      'description'          => __( 'Post Type Description', 'enforevo' ),
      'labels'                => $labels,
      'supports'              => array( 'title','thumbnail' ),
      'taxonomies'            => array( 'category' ),
      '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'      => 'page',
   );
   register_post_type( 'custom_post_type', $args );

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

Code:
<div class="aprblock-wrap">
            <?php
                    $category = new WP_Query(array('post_type' => 'custom_post_type', 'posts_per_page' => 6 ));
                   
                    while ( $category->have_posts() ) : $category->the_post();
                    ?>
                        <div class="aprblock-body">
                            <div class="aprblock-image">
                                <img src="<?= get_the_post_thumbnail_url() ?>" alt="">
                            </div>
                            <div class="aprblock-content">
                                    <div class="aprblock-title"><?php the_title(); ?></div>
                                    <div class="aprblock-address"><?= esc_attr( get_post_meta( get_the_ID(), 'phoenix_address', true ) ); ?></div>
                                    <div class="aprblock-contact"><?= esc_attr( get_post_meta( get_the_ID(), 'phoenix_contact', true ) ); ?></div>
                                  <!-- <div class="aprblock-facebook"><?= esc_attr( get_post_meta( get_the_ID(), 'phoenix_facebook', true ) ); ?></div> -->
                            </div>
                        </div>
   
                <?php endwhile; ?>
            </div>      
Xem chủ đề cũ hơnVề Đầu TrangXem chủ đề mới hơn
Permissions in this forum:
Bạn không có quyền trả lời bài viết