Custom Post Type

(Ответов: 0, Просмотров: 274)
  1. Опытный
    • Регистрация: 27.03.2015
    • Сообщений: 271
    • Репутация: 35
    Доброго дня!
    С Custom Post Type знаком относительно недавно.
    Создаю тип записи с яхтами:
    В fuctions.php добавил
    PHP код:
    require_once(TEMPLATEPATH '/admin/yachts.php');
    require_once(
    TEMPLATEPATH '/admin/tickets.php'); 
    и
    PHP код:
    /* SAVE */
    add_action('save_post''save_meta');
    function 
    save_meta(){
      global 
    $post;
      
    update_post_meta($post->IDprice$_POST["price"]);
      
    update_post_meta($post->IDmodel$_POST["model"]);
      
    update_post_meta($post->IDarea$_POST["area"]);
      
    update_post_meta($post->IDsleeping$_POST["sleeping"]);
      
    update_post_meta($post->IDcabins$_POST["cabins"]);
      
    update_post_meta($post->IDbathrooms$_POST["bathrooms"]);
      
    update_post_meta($post->IDlength$_POST["length"]);
      
    update_post_meta($post->IDbeam$_POST["beam"]);
      
    update_post_meta($post->IDyear$_POST["year"]);
      
    update_post_meta($post->IDequipment$_POST["equipment"]);
      
    update_post_meta($post->IDwater$_POST["water"]);
      
    //Tickets
      
    update_post_meta($post->IDpricetic$_POST["pricetic"]);

    файл /admin/yachts.php

    Развернуть текст


    PHP код:
    <?php
    add_action
    'init''yacht_register' );
    function 
    yacht_register() {
      
    $labels = array(
        
    'name' => _x('Yachts''post type general name'),
        
    'singular_name' => _x('Yacht''post type singular name'),
        
    'add_new' => _x('Add New''Yacht'),
        
    'add_new_item' => __('Add New Yacht'),
        
    'edit_item' => __('Edit Yacht'),
        
    'new_item' => __('New Yacht'),
        
    'all_items' => __('All Yachts'),
        
    'view_item' => __('View Yachts'),
        
    'search_items' => __('Search Yachts'),
        
    'not_found' =>  __('No yacht found'),
        
    'not_found_in_trash' => __('No yacht found in Trash'), 
        
    'parent_item_colon' => '',
        
    'menu_name' => 'Yachts'
      
    );
      
    $args = array(
        
    'labels' => $labels,
        
    'public' => true,
        
    'publicly_queryable' => true,
        
    'show_ui' => true
        
    'show_in_menu' => true
        
    'query_var' => true,
        
    'rewrite' => true,
        
    'capability_type' => 'post',
        
    'hierarchical' => false,
        
    'supports' => array( 'title''editor''thumbnail''page-attributes' )
      );
      
    register_post_type('yacht',$args);
    }
    // price //
    add_action("admin_init""price_meta");
     function 
    price_meta(){
      
    add_meta_box("price""Price""Price""yacht""normal""low");
    }
     function 
    price(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $price $custom["price"][0];
      
    ?>
      <label>Price:</label>
      <input id="price" name="price" type="text"  value="<?php echo $price?>" />
      <?php
    }

    // Builder / Model //
    add_action("admin_init""model_meta");
     function 
    model_meta(){
      
    add_meta_box("model""Builder/Model""model""yacht""normal""low");
    }
     function 
    model(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $model $custom["model"][0];
      
    ?>
      <label>Builder / Model:</label>
      <input id="model" name="model" type="text"  value="<?php echo $model?>" />
      <?php
    }

    // Area //
    add_action("admin_init""area_meta");
     function 
    area_meta(){
      
    add_meta_box("area""Area""area""yacht""normal""low");
    }
     function 
    area(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $area $custom["area"][0];
      
    ?>
      <label>Area:</label>
      <input id="area" name="area" type="text"  value="<?php echo $area?>" />
      <?php
    }

    // Sleeping capacity //
    add_action("admin_init""sleeping_meta");
     function 
    sleeping_meta(){
      
    add_meta_box("sleeping""Sleeping capacity""sleeping""yacht""normal""low");
    }
     function 
    sleeping(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $sleeping $custom["sleeping"][0];
      
    ?>
      <label>Sleeping capacity:</label>
      <input id="sleeping" name="sleeping" type="text"  value="<?php echo $sleeping?>" />
      <?php
    }

    // Cabins //
    add_action("admin_init""cabins_meta");
     function 
    cabins_meta(){
      
    add_meta_box("cabins""Cabins""cabins""yacht""normal""low");
    }
     function 
    cabins(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $cabins $custom["cabins"][0];
      
    ?>
      <label>Cabins:</label>
      <input id="cabins" name="cabins" type="text"  value="<?php echo $cabins?>" />
      <?php
    }

    // Bathrooms //
    add_action("admin_init""bathrooms_meta");
     function 
    bathrooms_meta(){
      
    add_meta_box("bathrooms""Bathrooms""bathrooms""yacht""normal""low");
    }
     function 
    bathrooms(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $bathrooms $custom["bathrooms"][0];
      
    ?>
      <label>Bathrooms:</label>
      <input id="bathrooms" name="bathrooms" type="text"  value="<?php echo $bathrooms?>" />
      <?php
    }

    // Length //
    add_action("admin_init""length_meta");
     function 
    length_meta(){
      
    add_meta_box("length""Length""length""yacht""normal""low");
    }
     function 
    length(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $length $custom["length"][0];
      
    ?>
      <label>Length:</label>
      <input id="length" name="length" type="text"  value="<?php echo $length?>" />
      <?php
    }

    // Beam //
    add_action("admin_init""beam_meta");
     function 
    beam_meta(){
      
    add_meta_box("beam""Beam""beam""yacht""normal""low");
    }
     function 
    beam(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $beam $custom["beam"][0];
      
    ?>
      <label>Beam:</label>
      <input id="beam" name="beam" type="text"  value="<?php echo $beam?>" />
      <?php
    }

    // Year //
    add_action("admin_init""year_meta");
     function 
    year_meta(){
      
    add_meta_box("year""Year""year""yacht""normal""low");
    }
     function 
    year(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $year $custom["year"][0];
      
    ?>
      <label>Year:</label>
      <input id="year" name="year" type="text"  value="<?php echo $year?>" />
      <?php
    }

    // Equipment //
    add_action("admin_init""equipment_meta");
     function 
    equipment_meta(){
      
    add_meta_box("equipment""Equipment""equipment""yacht""normal""low");
    }
     function 
    equipment(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $equipment $custom["equipment"][0];
      
    ?>
      <label>Equipment:</label>
      <input id="equipment" name="equipment" type="text"  value="<?php echo $equipment?>" />
      <?php
    }

    // Water Toys //
    add_action("admin_init""water_meta");
     function 
    water_meta(){
      
    add_meta_box("water""Water Toys""water""yacht""normal""low");
    }
     function 
    water(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $water $custom["water"][0];
      
    ?>
      <label>Water Toys:</label>
      <input id="water" name="water" type="text"  value="<?php echo $water?>" />
      <?php
    }




    /* Taxonomy */
    $labels = array(
        
    'name' => _x'Yachts Categories''taxonomy general name' ),
        
    'singular_name' => _x'Yacht Category''taxonomy singular name' ),
        
    'search_items' =>  __'Search Yacht Categories' ),
        
    'all_items' => __'All Yacht Categories' ),
        
    'parent_item' => __'Parent Category' ),
        
    'parent_item_colon' => __'Parent Category:' ),
        
    'edit_item' => __'Edit Yacht Category' ), 
        
    'update_item' => __'Update Yacht Category' ),
        
    'add_new_item' => __'Add Yacht Category' ),
        
    'new_item_name' => __'New Yacht Category' ),
        
    'menu_name' => __'Yacht Categories' )
      );    
     
    register_taxonomy('yachts',array('yacht'), array(
        
    'hierarchical' => true,
        
    'labels' => $labels,
        
    'query_var' => true,
        
    'show_ui' => true
     
    ));
    ?>
    [свернуть]


    На странице яхт вывожу всю необходию инфу. Все ок.
    Делаю по аналогии с яхтами тип записи с билетами (admin/tickets.php)
    В админке поля появляются, но вывести на странице с билетами не могу.

    Развернуть текст


    PHP код:
    <?php
    add_action
    'init''ticket_register' );
    function 
    ticket_register() {
      
    $labels = array(
        
    'name' => _x('Tickets''post type general name'),
        
    'singular_name' => _x('Ticket''post type singular name'),
        
    'add_new' => _x('Add New''Ticket'),
        
    'add_new_item' => __('Add New Ticket'),
        
    'edit_item' => __('Edit Ticket'),
        
    'new_item' => __('New Ticket'),
        
    'all_items' => __('All Tickets'),
        
    'view_item' => __('View Tickets'),
        
    'search_items' => __('Search Tickets'),
        
    'not_found' =>  __('No ticket found'),
        
    'not_found_in_trash' => __('No ticket found in Trash'), 
        
    'parent_item_colon' => '',
        
    'menu_name' => 'Tickets'
      
    );
      
    $args = array(
        
    'labels' => $labels,
        
    'public' => true,
        
    'publicly_queryable' => true,
        
    'show_ui' => true,
        
    'show_in_menu' => true,
        
    'query_var' => true,
        
    'rewrite' => true,
        
    'capability_type' => 'post',
        
    'hierarchical' => false,
        
    'supports' => array( 'title''editor''thumbnail''page-attributes' )
      );
      
    register_post_type('ticket',$args);
    }
    // price //
    add_action("admin_init""pricetic_meta");
     function 
    pricetic_meta(){
      
    add_meta_box("pricetic""Price""Price""ticket""normal""low");
    }
     function 
    pricetic(){
      global 
    $post;
      
    $custom get_post_custom($post->ID);
      
    $pricetic $custom["pricetic"][0];
      
    ?>
      <label>Price:</label>
      <input id="pricetic" name="pricetic" type="text"  value="<?php echo $pricetic?>" />
      <?php
    }



    /* Taxonomy */
    $labels = array(
        
    'name' => _x'Tickets Categories''taxonomy general name' ),
        
    'singular_name' => _x'Ticket Category''taxonomy singular name' ),
        
    'search_items' =>  __'Search Ticket Categories' ),
        
    'all_items' => __'All Ticket Categories' ),
        
    'parent_item' => __'Parent Category' ),
        
    'parent_item_colon' => __'Parent Category:' ),
        
    'edit_item' => __'Edit Ticket Category' ), 
        
    'update_item' => __'Update Ticket Category' ),
        
    'add_new_item' => __'Add Ticket Category' ),
        
    'new_item_name' => __'New Ticket Category' ),
        
    'menu_name' => __'Ticket Categories' )
      );

    register_taxonomy('tickets',array('ticket'), array(
        
    'hierarchical' => true,
        
    'labels' => $labels,
        
    'query_var' => true,
        
    'show_ui' => true
     
    ));
    ?>
    [свернуть]



    зы:
    вывожу так
    PHP код:
    <?php if( get_post_meta($post->ID'price'true)): ?>
    Price: <?php echo get_post_meta($post->ID'price'true); ?>
    <?php 
    endif; ?>
    Подскажите плз, в чем может быть причина?
    Спасибо!
    Последний раз редактировалось prihod; 30.10.2015 в 15:09.
    • 0

Похожие темы

Темы Раздел Ответов Последний пост
Не обновляются доп поля (custom fields) при изменении товара
WordPress 0 20.06.2014 01:19
Поиск по Custom fields virtuemart2
Joomla 1 05.03.2014 21:57
DLE тегом {custom} вывести рейтинговые статьи за неделю
DLE 7 04.05.2013 14:04
Валидность ошибка document type does not allow element "link" here
Web программирование 1 06.06.2012 18:52

У кого попросить инвайт?

Вы можете попросить инвайт у любого модератора:

Информеры