Wordpress the_content无法使用自定义帖子类型

时间:2013-07-15 14:59:25

标签: php wordpress wordpress-theming custom-post-type

使用自定义帖子类型创建自定义wordpress主题:

我添加了自定义帖子类型(通过插件自定义帖子UI)和自定义字段(通过高级自定义字段)。自定义帖子正在显示在创建的新模板页面中(single-custom.php)。因此,各个自定义帖子的显示方式完全符合我的要求。

问题是,在主页上只显示标题。

我做了什么:

1)我添加了以下代码,允许通过functions.php将新帖子类型提取到主页/博客Feed的Feed中:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

if ( ( is_home() && $query->is_main_query() ) || is_feed() )
    $query->set( 'post_type', array( 'post', 'custom' ) );

return $query;

感谢此{/ 3>的Justin Tadlock

2)我创建了一个自定义内容页面content-custom.php并修改了索引循环以调用该模板:

<?php if ( have_posts() ) : ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

        <?php
           get_template_part( 'content', 'custom', get_post_format() );
        ?>

    <?php endwhile; ?>

我的主页仍然只显示我的自定义帖子的标题,而不是我希望的完整内容。这个问题与content-custom.php中的the_content的调用有关,但我无法找到问题,因为我仍然熟悉wordpress。 content-custom.php的相关代码如下:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
            //The title below is displayed correctly
    <h1 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>  

</header><!-- .entry-header -->

    // The problem begins below. The entry-content div is created but is empty and is not pulling any content from the custom post through the_content call.

<div class="entry-content">
    <?php the_content(); ?>
</div><!-- .entry-content -->

2 个答案:

答案 0 :(得分:1)

我不熟悉您提到的插件,但请检查自定义帖子类型是否确实包含“内容”字段。

如果“内容”是自定义字段,则必须使用

get_post_meta($post->ID, 'field name', true);

我有一个类似的问题,通过在模板中添加以下内容来修复它。您也可以尝试添加到content-custom.php:

<?php wp_reset_postdata();  ?>

答案 1 :(得分:1)

某些与帖子相关的数据默认情况下不适用于get_posts ,例如通过the_content()发布内容或数字ID。这是通过调用内部函数 setup_postdata() 解决的,其中 $ post 数组作为其参数:

<?php
$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
  setup_postdata( $post ); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; 
wp_reset_postdata();
?>

请参阅Access all post data

相关问题