自定义Wordpress帖子类型和功能不显示图像或其他后期数据

时间:2014-08-28 23:24:31

标签: php html wordpress

我无法从此帖子类型中获取附件以在下面的函数中回显。呈现空白框并且未设置网址。任何想法都会有所帮助。我试图在主页上显示此功能,但不断获得相同的结果。

// 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
  }

1 个答案:

答案 0 :(得分:0)

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

但你可以做get_post()而不是返回帖子对象。