Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

another custom loop question WordPress


This code is being used on a page to display my recent posts, both normal and custom post-types.

If within my loop the post being displayed is a custom post-type 'photogallery' then return an unordered list of all the image attachments belonging to that post. If the post is not a photogallery post then style it normally.

I have a custom loop with an array of post types as follows:

if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
$paged = 1;
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts( array(
'post_type' => array('post','audio', 'photogallery'),
'paged' => $paged ));

<?php if (have_posts()) : $count = 0; ?>
<?php while (have_posts()) : the_post(); $count++; ?>

<!-- do stuff -->

<?php endwhile; else: ?>
<?php endif; ?>

What I want:

In place of <!-- do stuff -->

I want to say
[if_post_type =="photogallery" then give me an ascending unordered list of the image attachments that belong to that post ---else--- do stuff (style normally) ]

2 problems I have:

a. is_post_type == does not work, I need a function that will check current post's type
b. How to return an unordered list of the attachments that belong to that post in the loop.

What I am after is simple:
This would allow me to create a slideshow gallery out of the 'photogallery' posts within my loop and then simply style my post and audio posts-types as normal.

Please provide me with a modified vs. of my loop that will do the return of attachments for custom post-type photogallery, and if not photogallery just do stuff.

It is 1:30am here, won't check answers until tomorrow, so don't feel ignored if you answer and don't hear back right away. I'm just to tired of reading through the codex and finding no answers.

Answers (2)


Pippin Williamson answers:

Replace <!--do stuff--> with this:

<?php if( get_post_type() == 'photogallery' ) {
// photogallery stuff here
} else if( get_post_type() == 'audio' ) {
// audio stuff here
} else {
// regular post stuff here
} ?>


rilwis answers:

Here's the code (place in <!--do stuff-->):

if (get_post_type() == 'photogallery') {
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => null,
'post_parent' => get_the_ID()

$attachments = get_posts($args);

if ($attachments) {
echo '<ul>';
foreach ($attachments as $attachment) {
echo '<li>';
the_attachment_link($attachment->ID, false);
echo '</li>';
echo '</ul>';
} else {
// regular loop here