Posts module - displaying posts
Created 8 years ago by idlethumbsI'd like to know how to go about retrieving and displaying posts, and post types on a page and iterating through them.
Hey there!
Being that everything in PyroCMS is a Stream - you can use the entries
and entry
plugin functions to fetch / iterate over them in Twig.
Try something like this:
{% for post in entries('posts', 'posts').recent().get() %}
{{ html_link(post.path, post.title) }}
{% endfor %}
@rickkuk thanks for quick reply, do i need to install it to see this? I was trying to find an example on https://github.com/pyrocms/starter-theme/blob/master/resources/views/layouts/ maybe you can point out what i've missed
Note that recent
is a criteria method which is what is returned by the function. It basically wraps the query builder (see docs on criteria too). https://github.com/anomalylabs/posts-module/blob/master/src/Post/PostCriteria.php#L36
thanks Ryan, I was looking at this code snippet and miraculously still got it wrong... How do I get a new post type, say I've created a post type called 'testimoinal' ?
I think i may have it, should I just add an {% if post.slug = "testimonial" %} or is there a higher level filter?
You can use something like this to restrict by post type (refer to the post criteria again): {{ entries('posts', 'posts').type('testimonial').get() }}
.
is there a way to restrict what is looped over by the built in featured FT for posts? .featured('true')
or 1 or whatever needed to check for?
For the short and quick answer, take a look at the starter theme that comes with pyro. The home page displays a post.