最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Some custom post types did not return an object

programmeradmin4浏览0评论

I wrote a function to register custom post types below - appetizer (working well) - vermicelli (working well) - pho (working well) - seafoodNoodle (X) - koreanSpecial (X) - koreanRice (X) - lunchBox (X) - specialBox (X)

upper three post types are working well but others are not with functions such as - get_post_type_object($post_type) - get_post_type_archive_link($post_type)

phou-post-types.php

<?php 

  function phou_post_types() {
    register_post_type('appetizer', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'appetizers'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Appetizers',
        'add_new_item' => 'Add New Appetizer',
        'edit_item' => 'Edit Appetizer',
        'all_items' => 'All Appetizers',
        'singular_name' => 'Appetizer'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('vermicelli', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'vermicellis'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Vermicellis',
        'add_new_item' => 'Add New Vermicelli',
        'edit_item' => 'Edit Vermicelli',
        'all_items' => 'All Vermicellis',
        'singular_name' => 'Vermicelli'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('pho', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'phos'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Phos',
        'add_new_item' => 'Add New Pho',
        'edit_item' => 'Edit Pho',
        'all_items' => 'All Phos',
        'singular_name' => 'Pho'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('seafoodNoodle', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'seafoodNoodles'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Seafood Noodles',
        'add_new_item' => 'Add New Seafood Noodle',
        'edit_item' => 'Edit Seafood Noodle',
        'all_items' => 'All Seafood Noodles',
        'singular_name' => 'Seafood Noodle'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('koreanSpecial', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'koreanSpecials'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Korean Specials',
        'add_new_item' => 'Add New Korean Special',
        'edit_item' => 'Edit Korean Special',
        'all_items' => 'All Korean Specials',
        'singular_name' => 'Korean Special'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('koreanRice', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'koreanRices'),
      'has_archive' => true,
      'public' => true, 
      'exclude_from_search' => false,
      'labels' => array(
        'name' => 'Korean Rices',
        'add_new_item' => 'Add New Korean Rice',
        'edit_item' => 'Edit Korean Rice',
        'all_items' => 'All Korean Rices',
        'singular_name' => 'Korean Rice'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('lunchBox', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'lunchBoxes'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Lunch Boxes',
        'add_new_item' => 'Add New Lunch Box',
        'edit_item' => 'Edit Lunch Box',
        'all_items' => 'All Lunch Boxes',
        'singular_name' => 'Lunch Box'
      ),
      'menu_icon' => 'dashicons-carrot'
    ));

    register_post_type('specialBox', array(
      'supports' => array('title', 'editor'),
      'rewrite' => array('slug' => 'specialBoxes'),
      'has_archive' => true,
      'public' => true, 
      'labels' => array(
        'name' => 'Sepcial Boxes',
        'add_new_item' => 'Add New Sepcial Box',
        'edit_item' => 'Edit Sepcial Box',
        'all_items' => 'All Sepcial Boxes',
        'singular_name' => 'Sepcial Box'
      ),
      'menu_icon' => 'dashicons-carrot'
    )); 
  } 

  add_action('init', 'phou_post_types');
?>

functions.php

function home_menu($args) {
  $post_type = $args['post_type'];
  $post_type_obj = get_post_type_object($post_type);
  if ($post_type_obj) {
    print_r($post_type_obj);
  } else {
    print_r('there isnt object');
  }
  ?>

  <div class="col-xs-12 col-sm-6">
    <div class="menu-section">
      <h2 class="menu-section-title">
        <?php echo $post_type_obj->labels->singular_name; ?></h2> //did not return any name
      <hr>
      <?php 
        $menus = new WP_Query(array(
          'posts_per_page' => -1,
          'post_type' => $post_type,
          'orderby' => 'menu_number',
          'order' => 'ASC'
        ));
        while($menus->have_posts()) {
          $menus->the_post();
        ?>
        <a href="<?php echo get_post_type_archive_link($post_type); ?>">
        <div class="menu-item">
          <div class="menu-item-name"><?php the_title(); ?></div>
          <div class="menu-item-price"><?php the_field('price'); ?></div>
          <div class="menu-item-description"><?php 
            $diets = get_field('diet');
            if($diets) {
              foreach($diets as $diet) {
                echo $diet." ";
              } 
            }
            ?></div>
        </div></a>
        <?php } ?>
      </div>
    </div>
  <?php 
}

index.php

<!-- Restaurant Menu Section -->
<div id="restaurant-menu">
  <div class="section-title text-center center">
    <div class="overlay">
      <h2>Menu</h2>
      <hr>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit duis sed.</p>
    </div>
  </div>

  <div class="container">

    <div class="row">
      <?php
      home_menu(array(
        'post_type' => 'appetizer'
      )); ?>
      <?php
      home_menu(array(
        'post_type' => 'vermicelli'
      )); ?>
    </div>

    <div class="row">      
    <?php
      home_menu(array(
        'post_type' => 'pho'
      )); ?>
      <?php
      home_menu(array(
        'post_type' => 'seafoodNoodle'
      )); ?>      
    </div>

    <div class="row">      
    <?php
      home_menu(array(
        'post_type' => 'koreanSpecial'
      )); ?>
      <?php
      home_menu(array(
        'post_type' => 'koreanRice'
      )); ?>      
    </div>

    <div class="row">      
    <?php
      home_menu(array(
        'post_type' => 'lunchBox'
      )); ?>
      <?php
      home_menu(array(
        'post_type' => 'specialBox'
      )); ?>      
    </div>

  </div>
</div>
发布评论

评论列表(0)

  1. 暂无评论