如何显示WordPress自定义帖子类型的列表?

如何显示WordPress自定义帖子类型的列表?

问题描述:

我使用的是WordPress 3,我创建了一个名为article的自定义帖子类型,它为我提供了mywebsite/articles/article-title的url格式。如何查看url mywebsite/articles中的所有文章条目?如何显示WordPress自定义帖子类型的列表?

假设您正确设置了所有内容,并希望在公共模板页面上看到帖子类型,请将此代码放入mycustomtemplate.php或eqivilate。

<?php $loop = new WP_Query(array('post_type' => 'article', 'posts_per_page' => 10)); ?> 

<?php while ($loop->have_posts()) : $loop->the_post(); ?> 

    <?php the_title('<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '" rel="bookmark">', '</a></h2>'); ?> 

    <div class="entry-content"> 
     <?php the_content(); ?> 
    </div> 
<?php endwhile; ?> 

您可以像定制博客文章和页面一样自定义循环。如果你想在一个页面上获得所有,你会想删除post_per_page上的10的限制,但我不会建议它。我会说设置为50或100,并仍然使用页面。

来源:http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

+2

如何使mywebsite /文章使用模板mycustomtemplate.php? – 2010-08-04 02:58:58

+1

当你设置'register_post_type'变量时,你应该可以向你的数组添加''rewrite'=>数组('slug'=>'articles','with_front'=> false)'。查看我上面发布的链接。 – BandonRandon 2010-08-04 21:40:54

+0

你也应该可以添加像single-mycustomtemplate.php – BandonRandon 2010-08-04 21:46:35

您可以轻松地从你的自定义文章类型的定义实现这一目标,从WP 3.1。只需将has_archive设置为true即可。

来源:http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types

我有最简单的解决方案。只要创建文件archive-{custom post type}.php 然后,就像平常一样做循环内容。

如果你已经设置的永久链接,只需键入yourdomain.com/{custom post type}

+1

这个工程很好。但是,我发现在创建新的自定义帖子类型之后,我必须进入设置>固定链接并单击保存,以某种方式刷新永久链接的缓存。否则'yourdomain.com/{custom post type}'会给出404错误。希望能帮助别人。 – 2014-07-03 08:51:50

+0

即使遵循Simons指令,我仍然可以获得404。我将'has_archive'注册的自定义帖子类型设置为true。 – AndrewJM 2014-09-15 15:23:46

+0

'如果你已经设置了永久链接'一个人怎么做?我也看到404也不知道如何创建一个页面来显示我的自定义帖子类型的帖子列表** – Ejaz 2014-09-16 23:03:10