我在Admin中创建了带有3个字段(名称、标题、技术)的自定义post类型" Projects“,并添加了一个项目列表。
我想在我的自定义主题中显示项目列表。
你能给我一个更好的理解和整合的参考吗?
发布于 2017-04-05 20:00:36
您希望获得一系列的帖子,仅限于您的自定义post类型。我会用get_posts()。
$args = array(
'posts_per_page' => -1, // -1 here will return all posts
'post_type' => 'project', //your custom post type
'post_status' => 'publish',
);
$projects = get_posts( $args );
foreach ($projects as $project) {
printf('<div><a href="%s">%s</a></div>',
get_permalink($project->ID),
$project->post_title);
}发布于 2017-04-13 14:20:58
我会使用‘查询’来进行查询并显示结果:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //pagination
$args = array(
'paged' => $paged,
'posts_per_page' => 12, //or any other number
'post_type' => 'Projects' //your custom post type
);
$the_query = new WP_Query( $args ); // The Query
if ( $the_query->have_posts() ) { // The Loop
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>'; //shows the title of the post (Project)
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}这段代码在无序列表中显示“项目”,但是您可以使用任何其他HTML (div,ol,文章.)
https://stackoverflow.com/questions/43239766
复制相似问题