【问题标题】:custom post type not displaying自定义帖子类型不显示
【发布时间】:2017-03-12 09:32:57
【问题描述】:

我创建了一个名为 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(); ?>

【问题讨论】:

    标签: php wordpress post types


    【解决方案1】:

    get_posts() 返回帖子数组,不只有一个帖子。 get_post_meta() 第一个参数需要单个帖子的 ID,而不是帖子 ID 的数组。

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

    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();
    

    【讨论】:

    • 我现在需要做的就是找出完整的列表,而不是单独的大声欢呼 m8
    • 使用循环生成完整列表。
    • 在 movies.php 或 ?对不起,我有点业余:(
    • $posts = get_posts(array('post_type' =&gt; 'movies','post_status' =&gt; 'publish','posts_per_page' =&gt; -1,'fields' =&gt; 'ids')); 你在你的代码中使用了这个函数。它将所有电影 ID 的数组创建到 $posts 变量中。要打印所有这些,您必须使用循环。试试我上面发布的代码
    • 我了解循环并且我之前有一个循环,但它在一个电影帖子中输出所有电影元数据的所有信息。所以什么文件是我改变PLZ。对不起,我是个菜鸟 :)
    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多