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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3398432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:37:18  来源:igfitidea点击:

How can I display a list of WordPress custom posts?

wordpresscustom-post-type

提问by Giljed Jowes

I am using WordPress 3, and I created a custom post type called article, which gives me the URL format of mywebsite/articles/article-title. How do I see all the article entries in the URL mywebsite/articles?

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

回答by Brooke.

Assuming you set up everything correctly and you want to see the post type on a public template page, try this code into mycustomtemplate.php or the equivalent.

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

<?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; ?>

You can customize the loop just like you would blog posts and pages. If you want to get allon one page you'll want to remove the limit of 10 on post_per_pageI wouldn't suggest it though. I would say set it to 50 or 100 and still use pages.

您可以像处理博客文章和页面一样自定义循环。如果您想将所有内容都放在一页上,您需要取消 10 的限制,post_per_page但我不建议这样做。我会说将其设置为 50 或 100 并且仍然使用页面。

Source: Custom post types in WordPress

来源:WordPress 中的自定义帖子类型

回答by Thobari Ibnu

I have the simplest solution. Just create file archive-{custom post type}.phpand then, just do loop content as usual.

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

If you already set the permalink, just type yourdomain.com/{custom post type}.

如果您已经设置了永久链接,只需输入yourdomain.com/{custom post type}.

回答by Vlad Socaciu

You can easily achieve this from the definition of your custom post type, starting with WP 3.1. Just set has_archiveto true.

从 WP 3.1 开始,您可以通过自定义帖子类型的定义轻松实现这一点。只需设置has_archivetrue.

Source: http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types

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