自定义帖子类型不显示

时间:2016-10-29 19:33:19

标签: php wordpress post types

我制作了一个名为movies的自定义帖子类型,并添加了一个电影元框,其中包含一些我希望在帖子中看到的关于我要添加到存档的电影的选项。

问题是我可以在一个帖子中查看所有元信息,但不能查看我在single-movie.php

中查看的一部电影的具体元信息
<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div class="panel panel-default panel-body">

    <p class="fa fa-calendar"></p>&nbsp;<?php the_time('d-m-Y'); ?> | <p class="fa fa-clock-o"></p> <?php the_time('H:i a'); ?> |&nbsp;<p class="fa fa-user"></p>&nbsp;<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></i> |&nbsp;<p class="fa fa-envelope-o"></p>&nbsp;posted in movies

<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?> clearfix">

    <article class="post-thumbnail">

        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('image-poster'); ?></a>

    </article>

    <article class="post-thumbnail-text">

        <h4 class="align_Center" style="color:#000; padding-bottom:1em;"><?php the_title(); ?></h4>

        <?php $posts = get_posts(array(
            'post_type'   => 'movies',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'fields' => 'ids'
            )
        );

            $nam = get_post_meta( $posts, "name", true );
            $rel = get_post_meta( $posts, "released", true );
            $rat = get_post_meta( $posts, "rating", true );
            $dur = get_post_meta( $posts, "duration", true );
            $gen = get_post_meta( $posts, "genrer", true );
            $dir = get_post_meta( $posts, "director", true );
            $wri = get_post_meta( $posts, "writer", true );
            $sta = get_post_meta( $posts, "stars", true );
            $sto = get_post_meta( $posts, "storyline", true );

            ?>

            <div class="align_Left">
                <p>Title:</p>&nbsp;<?php echo $nam; ?><br>
                <p>Released:</p>&nbsp;<?php echo $rel; ?><br>
                <p>Rating:</p>&nbsp;<?php echo $rat; ?><br>
                <p>Duration:</p>&nbsp;<?php echo $dur; ?><br>
                <p>Genre:</p>&nbsp;<?php echo $gen; ?><br>
                <p>Director:</p>&nbsp;<?php echo $dir; ?><br>
                <p>Writer:</p>&nbsp;<?php echo $wri; ?><br>
                <p>Stars:</p>&nbsp;<?php echo $sta; ?><br>
                <p>Storyline:</p>&nbsp;<?php echo $sto; ?>
            </div>

    </article>

</article>

</div>

<?php endwhile;

else :

    echo '<p>NO CONTENT FOUND</p>';

endif; ?>

</div><!-- ./CONTAINER --> 

<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

get_posts()返回的帖子数量不仅仅有一个帖子。第一个参数的get_post_meta()需要单个帖子的ID而不是帖子数组&#39; IDS。

您需要做的是创建循环,例如:

foreach ( $posts as $post ) : setup_postdata( $post );
  $name = get_post_meta( get_the_ID(), "name", true ); ?>
  ...
  <p>Title:</p>&nbsp;<?php echo $nam; ?><br>
  ...
<?php
endforeach; 
wp_reset_postdata();