查询自定义帖子类型并显示其内容

时间:2014-11-05 作者:Damir

我是一个开发wp主题的新手,在展示我的cpt时遇到了困难。

我在函数中添加了新的cpt。php文件:

// Creates Testimonials Custom Post Type
function testimonials_init() {
    $args = array(
      \'label\' => \'Testimonials\',
        \'public\' => true,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'testimonials\'),
        \'query_var\' => true,
        \'menu_icon\' => \'dashicons-format-quote\',
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'excerpt\',
            \'trackbacks\',
            \'custom-fields\',
            \'comments\',
            \'revisions\',
            \'thumbnail\',
            \'author\',
            \'page-attributes\',)
        );
    register_post_type( \'testimonials\', $args );
}
add_action( \'init\', \'testimonials_init\' );
我创建了一个新的页面模板,并添加了以下代码:

<?php 
 /*
 * Template Name: Testimonials
 */
 ?>

<?php get_header(); ?>
<div class="container">
<div class="content-page">
<h1><?php wp_title(); ?></h1>
<title><?php wp_title(); ?></title>
<?php
 $query = new WP_Query( array(\'post_type\' => \'testimonials\', \'posts_per_page\' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>


<?php endwhile; ?>
</div>
</div>

<?php 
$args = array( \'post_type\' => \'testimonials\', \'posts_per_page\' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?> 
</div>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>




<?php get_footer(); ?>
我希望我的页面在前端显示所有推荐信,但我无法做到这一点,因此出现以下错误:

Parse error: syntax error, unexpected \'else\' (T_ELSE)
谢谢大家!!!

1 个回复
最合适的回答,由SO网友:luke 整理而成

您没有关闭第二个while循环。你可能会想要这样的东西:

<?php 
 /*
 * Template Name: Testimonials
 */
 ?>

<?php get_header(); ?>

<div class="container">
    <div class="content-page">
        <h1><?php wp_title(); ?></h1>
        <title><?php wp_title(); ?></title>
        <?php
         $query = new WP_Query( array(\'post_type\' => \'testimonials\', \'posts_per_page\' => 5 ) );
         while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php endwhile; ?>
    </div>
</div>

<?php 
$args = array( \'post_type\' => \'testimonials\', \'posts_per_page\' => 10 );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="entry-content">
            <?php the_content(); ?> 
        </div>
    <?php endwhile; ?>
<?php else:  ?>
    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

<?php get_footer(); ?>

结束

相关推荐

Themes VS Plugins

如果我正在定制WordPress网站,我应该通过主题或插件来完成吗?所谓定制,我的意思是:添加新的自定义帖子类型,为用户添加新字段,但我想确认这是否是正确的方法。谢谢