Have you seen our video tutorials? Check 'em out!

Posts module - displaying posts

idlethumbs - 3 months ago

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

rickkuk - 3 months 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 - 3 months 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 - 3 months 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 - 3 months 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 - 3 months 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 - 3 months ago

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

ryanthompson - 3 months ago

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