Forums

[resolved] Getting posts from a specific post_format only (5 posts)

  1. Gatto Nero
    Member
    Posted 1 year ago #

    Hi all :)

    I'll try and be short (without being rude, I hope): I'm doing some heavy change to a wordpress theme - like uhm, doing it from scratch - and I'm really amazed at the potential of post_formats.

    What I'd like to do is getting *only* posts from a specific post_format (ie.: gallery) and add them - ok, just a thumbnail ;) - to my sidebar.

    I tried with get_posts, but I have no idea which parameter should I use to get them, so... Documentation seems not to mention post_format, in these cases...

    Any Idea?

    I tried something like

    <?php
    $args = array( 'numberposts' => 3, 'post_format' => gallery );
    $lastposts = get_posts( $args );
    foreach($lastposts as $post) : setup_postdata($post); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php endforeach; ?>

    Obviously, it doesn't work :)
    I suppose post formats are a special type of post_type, but... which is the "name" of them?

  2. Gatto Nero
    Member
    Posted 1 year ago #

    I'll add something... funny :)
    I tried to create a new category, called "gallery", without adding a specific slug.
    Wordpress automagically created the slug "post-format-gallery".

    (Obviously, I'm not able to make get_posts function with category_name, but...)

  3. Gatto Nero
    Member
    Posted 1 year ago #

    Somehow, I did it.

    I wrote the code here, in case someone needed it :)

    <?php
    $galleryposts = get_posts('category_name=post-format-gallery&post_count=4');
    foreach($galleryposts as $post) : setup_postdata($post); ?>
    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php endforeach; ?>

    What I've learnt:

    • The sample codes on the Codex don't work always
    • get_posts seems to work perfectly as wp_query... but don't use arrays :)
    • post-formats create a special category name called post-format-NAMEOFTHEFORMAT
  4. heddesheimer
    Member
    Posted 4 months ago #

    Hi Gatto,

    thanks for posting your solution here. It pointed me to the right direction. Anyway, your solution did not work for me, so it may be possible that it has been changed in the latest WP version. With WP 3.3.1 I can now do this:

    query_posts(array('post_format' => 'post-format-gallery',
            'category_name' => 'Photo Contest'));

    which works fine to get all posts from post format "Gallery" and category name "Photo Contest".

  5. blindscientist
    Member
    Posted 4 months ago #

    Hello All,
    Am I understanding this correctly? I need to have category-based blogroll on each of several different pages (based on the page's category). For example, the SPORTS page would have a blog-style listing of each post with the SPORTS category assigned to it (several writers working on one magazine site - each writing within his/her category, i.e., fashion, dining, etc). Does this code do that? If so, where? I know HTML and CSS but just learning php, etc. new to WP, so please excuse "noobness" of the question and point me in the right direction. Thanks in advance,

Topic Closed

This topic has been closed to new replies.

About this Topic