显示自定义主题中的自定义帖子类型列表
问题描述:
我在管理面板中创建了包含3个字段(名称,标题,技术)的自定义帖子类型“项目”并添加了项目列表。显示自定义主题中的自定义帖子类型列表
我想在我的自定义主题中显示项目列表。
你能给我一个更好的参考,以理解和集成
答
你想获得桩的阵列,仅限于您的自定义后的类型。我会使用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);
}
答
我会用'WP_Query“做一个查询并显示结果:
<?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,article ...)
可以得到特定的项目通过提及Id,Like Where条件。? – Developer