在首页上显示自定义帖子类型

时间:2018-07-23 09:32:11

标签: php wordpress

我正在创建自定义主题,并且正在使用自定义帖子类型,我试图循环自定义帖子类型以显示在页面上,但由于某些原因,该帖子未显示。

自定义帖子类型

/* Team Members */
function therabytes_team_post_type() {
    $labels = array (
        'name' => 'Team',
        'singular_name' => 'Team',
        'add_new' => 'Add Team Member',
        'all_items' => 'All Team Members',
        'add_new_item' => 'Add Team Member',
        'edit_item' => 'Edit Team Member',
        'new_item' => 'New Team Member',
        'view_item' => 'View Team Member',
        'search_item' => 'Search Team Member',
        'not_found' => 'Team member not found',
        'not_found_in_trash' => 'Item not found in trash',
        'parent_item_colon' => 'Parent Item',
    );

    $arg = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'supports' => array(
            'title',
            'revisions'
        ),
        'taxonomies' => array(),
        'menu_position' => 5,
        'exclude_from_search' => true
    );

    register_post_type('team', $arg);
}

add_action('init', 'therabytes_team_post_type');

增加自定义帖子类型

    <?php
    $teamargs = array( 'post_type' => 'team', 'posts_per_page' => 5 );
    $teamloop = new WP_Query( $teamargs );
    if(have_posts()) : ?>
    <?php
      while ( $teamloop->have_posts() ) : $teamloop->the_post();
    ?>
    <p>Custom post</p>
  <?php endwhile;?>
<?php endif;?>

模板页面

<?php
/*
  Template Name: Home Template
*/
get_header();
?>
<div class="team-carousel">
            <?php
            $teamargs = array( 'post_type' => 'team', 'posts_per_page' => 5 );
            $teamloop = new WP_Query( $teamargs );
            if(have_posts()) : ?>
            <?php
              while ( $teamloop->have_posts() ) : $teamloop->the_post();
            ?>
            <p>Custom post</p>
          <?php endwhile;?>
        <?php endif;?>

          <div class="flip-card">
            <div class="card team">
              <div class="front">
                <img src="<?php echo get_template_directory_uri(); ?>/images/team/Thorsten.png" alt="">
              </div>
              <div class="back team-info">
                <p class="team-name">Thorsten Feldmann</p>
                <p class="position">Founder/CEO</p>
              </div>
            </div>
          </div>
  </div>

    <?php
      get_footer();
    ?>

在这里,我要在模板页面上显示我要显示自定义帖子类型的页面,我一直在检查一些代码,并且我的字体找到了原因。

1 个答案:

答案 0 :(得分:0)

尝试此代码

&nbsp;