在首页上显示自定义文章类型



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

自定义帖子类型

/* 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();
?>

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

试试这个代码

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

相关内容

  • 没有找到相关文章

最新更新