自定义Wordpress Post类型和功能不显示图像或其他Post数据


Custom Wordpress Post Type and Function Not Displaying Images or Other Post Data

我无法从该帖子类型中获取附件,以便在下面的函数中进行回显。将呈现空白框,并且未设置URL。任何想法都会有所帮助。我试图让这个功能出现在主页上,但一直得到同样的结果。

// Custom Post Type
register_post_type('inf_home_items', array(
    'labels' => array(
            'name'   => 'Items of the Week',
            'singular_name' => 'Items This Week',
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New Items' ),
            'view_item' => 'View Items This Week',
            'edit_item' => 'Edit Story',
            'new_item' => __('New Items of The Week Story'),
            'view_item' => __('View Items of the Week Story'),
            'search_items' => __('Search Items of the Week Stories'),
            'not_found' =>  __('No entries found'),
            'not_found_in_trash' => __('No entries found in Trash'),
    ),
    'public' => true,
    'exclude_from_search' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    '_edit_link' =>  'post.php?post=%d',
    'rewrite' => array(
            "slug" => "homebox2",
            "with_front" => false,
    ),
    'query_var' => true,
    'supports' => array('title', 'editor', 'page-attributes', 'thumbnail'),
));
// Custom Fields
    Carbon_Container::factory('custom_fields', __('Items of The Week Options', 'inf'))
    ->show_on_post_type(array('post','inf_home_items'))
->add_fields(array(
            Carbon_Field::factory('text', 'item1_link_url', 'Item 1 URL'),
    Carbon_Field::factory('text', 'item2_link_url', 'Item 2 URL'),
            Carbon_Field::factory('text', 'item3_link_url', 'Item 3 URL'),
            Carbon_Field::factory('text', 'item4_link_url', 'Item 4 URL'),
            Carbon_Field::factory('text', 'item5_link_url', 'Item 5 URL'),
            Carbon_Field::factory('attachment', 'itemofweek1', 'First Item')
                    ->help_text('Image dimensions - 133 x 133 pixels.'),
            Carbon_Field::factory('attachment', 'itemofweek2', 'Second Item')
                    ->help_text('Image dimensions - 133 × 133 pixels.'),
            Carbon_Field::factory('attachment', 'itemofweek3', 'Third Item')
                    ->help_text('Image dimensions - 133 × 133 pixels.'),
            Carbon_Field::factory('attachment', 'itemofweek4', 'Fourth Item')
                    ->help_text('Image dimensions - 133 × 133 pixels.'),
            Carbon_Field::factory('attachment', 'itemofweek5', 'Fith Item')
                    ->help_text('Image dimensions - 133 × 133 pixels.')
         ));
// Items of The Week
function inf_items() {
  $args = array(
    'post_type' => 'inf_home_items',
    'posts_per_page' => 1
     );
 $list_items = get_posts($args);
 $title = get_the_title($list_item ->ID);
 $link1 = trim(carbon_get_post_meta($list_item->ID, 'item1_link_url'));
 $link2 = trim(carbon_get_post_meta($list_item->ID, 'item2_link_url'));
 $link3 = trim(carbon_get_post_meta($list_item->ID, 'item3_link_url'));
 $link4 = trim(carbon_get_post_meta($list_item->ID, 'item4_link_url'));
 $link5 = trim(carbon_get_post_meta($list_item->ID, 'item5_link_url'));
 $image1 = carbon_get_post_meta($list_item->ID, 'itemofweek1');
 $image2 = carbon_get_post_meta($list_item->ID, 'itemofweek2');
 $image3 = carbon_get_post_meta($list_item->ID, 'itemofweek3');
 $image4 = carbon_get_post_meta($list_item->ID, 'itemofweek4');
 $image5 = carbon_get_post_meta($list_item->ID, 'itemofweek5');
 ?>
<div class="favorites_container">
  <div class="favorites">
   <a href="<?php echo $link1; ?>">
    <img src="<?php echo $image1; ?>" height="133px" width="133px" />
   </a>
  </div>
<div class="favorites">
   <a href="<?php echo $link2; ?>">
    <img src="<?php echo $image2; ?>" height="133px" width="133px" />
   </a>
 </div>
<div class="favorites">
  <a href="<?php echo $link3; ?>">
   <img src="<?php echo $image3; ?>" height="133px" width="133px" />
  </a>
</div>
<div class="favorites">
  <a href="<?php echo $link4; ?>">
   <img src="<?php echo $image4; ?>" height="133px" width="133px" />
 </a>
</div>
 <div class="favorites">
    <a href="<?php echo $link5; ?>">
      <img src="<?php echo $image5; ?>" height="133px" width="133px" />
    </a>
  </div>
  <?php
  }

get_posts()返回一个Array,因此$list_item->ID变为$list_item[0]->ID

但是您可以执行get_post(),它将返回post对象。