Posts module - displaying posts
Created 8 years ago by idlethumbs

I'd like to know how to go about retrieving and displaying posts, and post types on a page and iterating through them.

rickkuk  —  8 years ago

For the short and quick answer, take a look at the starter theme that comes with pyro. The home page displays a post.

ryanthompson  —  8 years ago

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 %}
idlethumbs  —  8 years ago

@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

ryanthompson  —  8 years ago

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

idlethumbs  —  8 years ago

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

idlethumbs  —  8 years ago

I think i may have it, should I just add an {% if post.slug = "testimonial" %} or is there a higher level filter?

ryanthompson  —  8 years ago

You can use something like this to restrict by post type (refer to the post criteria again): {{ entries('posts', 'posts').type('testimonial').get() }}.

carsonsuite  —  6 years ago

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?