个性化阅读
专注于IT技术分析

WordPress the_content不适用于自定义帖子类型

点击下载

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

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

但是, 问题在于主页上仅显示标题。

我所做的:

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

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;

感谢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 -->

#1


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

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

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

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

<?php wp_reset_postdata();  ?>

#2


默认情况下, 某些与帖子相关的数据不可用于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();
?>

请参阅访问所有帖子数据

赞(0)
未经允许不得转载:srcmini » WordPress the_content不适用于自定义帖子类型

评论 抢沙发

评论前必须登录!