ساخت custom post type در وردپرس و متاهای آن
28 دی 1400
ارسال شده توسط رضا راد
189 بازدید
در این آموزش یاد میگیریم که چطور در وردپرس custom post type تعریف و برای این پست ها، متاهای مختص خودشون رو اضافه کنیم.
حتما ببینید (تکمیلی ویدیو بالا) : ساخت دسته بندی مختص پست تایپ (cpt) در وردپرس
<?php /** */ /* Plugin Name: Real Estate CPT Plugin URI: https://radwebdesign.ir Description: Real EstateCustom Post Type Version: 1.0 Author: Reza Rad Author URI: https://radwebdesign.ir Text Domain: real-estate */ function real_estate() { $labels = array( 'name' => _x('Properties', 'Post Type General Name', 'real-estate'), 'singular_name' => _x('Property', 'Post Type Singular Name', 'real-estate'), 'menu_name' => __('Properties', 'real-estate'), 'parent_item_colon' => __('Parent Property', 'real-estate'), 'all_items' => __('All Properties', 'real-estate'), 'view_item' => __('View Property', 'real-estate'), 'add_new_item' => __('Add New Property', 'real-estate'), 'add_new' => __('Add New Property', 'real-estate'), 'edit_item' => __('Edit Property', 'real-estate'), 'update_item' => __('Update Property', 'real-estate'), 'search_items' => __('Search Property', 'real-estate'), 'not_found' => __('Not Found', 'real-estate'), 'not_found_in_trash' => __('Not found in Trash', 'real-estate'), ); $args = array( 'label' => __('Real Estate', 'real-estate'), 'description' => __('Real Estate Properties', 'real-estate'), 'labels' => $labels, // Features this CPT supports in Post Editor 'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields',), // You can associate this CPT with a taxonomy or custom taxonomy. 'taxonomies' => array('genres'), /* A hierarchical CPT is like Pages and can have * Parent and child items. A non-hierarchical CPT * is like Posts. */ 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', 'show_in_rest' => true, 'register_meta_box_cb' => 'wpt_add_movie_metaboxes', ); register_post_type('realestate', $args); register_taxonomy('realestate-taxonomy', 'realestate', array( 'label' => __('دسته بندی املاک','real-estate'), 'rewrite' => array('slug' => 'realstate-category'), 'hierarchical' => true )); } add_action('init', 'real_estate'); /// Adding post meta function add_meta_realestate() { add_meta_box( 'propertymeta', 'متاهای املاک ', 'add_meta_inputs', 'realestate', 'side', // normal, side 'default' ); function add_meta_inputs() { global $post; // Nonce field to validate form request came from current site wp_nonce_field(basename(__FILE__), 'event_fields'); $area = get_post_meta($post->ID, 'area', true); $age = get_post_meta($post->ID, 'age', true); echo 'مساحت: <input type="text" name="area" value="' . esc_textarea($area) . '" class="widefat">'; echo 'سن ساختمان: <input type="text" name="age" value="' . esc_textarea($age) . '" class="widefat">'; } } add_action('add_meta_boxes', 'add_meta_realestate'); // Saving meta to database => wp-postmeta function save_post_meta($post_id, $post) { // Return if the user doesn't have edit permissions. if (! current_user_can('edit_post', $post_id)) { return $post_id; } // Verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times. if ((! isset($_POST['area']) && ! isset($_POST['age'])) || ! wp_verify_nonce($_POST['event_fields'], basename(__FILE__))) { return $post_id; } $metas['area'] = esc_textarea($_POST['area']); $metas['age'] = esc_textarea($_POST['age']); // meta ---------> ['area' => '', 'age' => ''] foreach ($metas as $key => $value): // Don't store custom data twice if ('revision' === $post->post_type) { return; } if (get_post_meta($post_id, $key, false)) update_post_meta($post_id, $key, $value); else add_post_meta($post_id, $key, $value); if (! $value) delete_post_meta($post_id, $key); endforeach; } add_action('save_post', 'save_post_meta', 1, 2);
درباره رضا راد
کارشناسی فناوری اطلاعات علاقمند به برنامه نویسی و طراحی وب سایت
نوشته های بیشتر از رضا رادمطالب زیر را حتما مطالعه کنید
Bitstarz para yatırma bonusu yok code australia, promo code bitstarz 2023
Bitstarz para yatırma bonusu yok code australia ...
Nordicbet casino no deposit bonus, tangiers casino
Nordicbet casino no deposit bonus Nordicbet casino no deposit bonus No...
Wonder cash casino free chips, states with regulated online casino games
Wonder cash casino free chips Wonder cash casino free chips Previous...
Bitstarz casino промокод 2023, битстарз otzoffinfo.club
Bitstarz casino промокод 2023 ...
Bitstarz, play n go bitcoin slot free
Bitstarz Bitstarz Bitstarz has...
Free spin casino sans dépôt, best online casinos in india
Free spin casino sans dépôt Free spin casino sans dépôt In...
دیدگاهتان را بنویسید