4

I've created a custom post type with categories and sub categories, what I need to do is list out the post titles and images for a given sub-category or category in a page template.

I've got as far as getting the all the listed items in the custom post type, but I'm unsure how to go further... any help appreciated.

<?php 
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>

The function that creates the custom post type and taxonomy looks like this:-

<?php

//      CUSTOM POST TYPE 1
add_action('init', 'portfolio_register');

function portfolio_register() {
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Portfolio'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail')
    );

    register_taxonomy("galleries", array("portfolio"), array(
        "hierarchical" => true, 
        "label" => "Galleries", 
        "singular_label" => "Galleries", 
        "rewrite" => true)
    );

    register_post_type( 'portfolio' , $args );
}


add_action("admin_init", "admin_init");
add_action('save_post', 'save_portfolio_options');
add_action('save_post', 'save_portfolio_single_options');

function admin_init(){
    add_meta_box("gallerymeta", "Gallery Options", "portfolio_meta_options", "portfolio", "normal", "low");
    add_meta_box("portfoliometa", "Portfolio Item Options", "portfolio_single_meta_options", "portfolio", "side", "low");
}

function portfolio_meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $excerpt = $custom["excerpt"][0];
    $info = $custom["info"][0];
    $linkto = $custom["linkto"][0];
?>
1
  • 2
    FYI, register_post_type has a register_meta_box_cb arg that accepts a callback function to register metaboxes applicable to that type(so you don't need to register then on the init action, which would be the incorrect action to register them on anyway)..
    – t31os
    Commented Jul 16, 2011 at 12:28

4 Answers 4

3
+50

This is a version of a function I'm using in the framework I'm working on, with example html replacing another function that contains something like it.

// Custom Loop

function arrr_custom_loop( $r_type = 'post', $r_post_num, $r_tax = 'category', $r_terms = 'featured' )  {
$args = array( 
    'showposts' => $r_post_num, 
    'tax_query' => array( 
        array( 
            'post_type' => $r_type,
            'taxonomy' => $r_tax, 
            'field' => 'slug', 
            'terms' => array( 
                $r_terms 
            ), 
        )
    )
);
query_posts( $args );
if (have_posts())
while ( have_posts() ) : the_post();
$more = 0;
?>
<article>
                <header class="pagetitle">
<?php if ( is_singular() )  { ?>
                    <h1><?php the_title(); ?></h1>
<?php } else { ?>
                    <h2 class="entry"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php } ?>
                </header>
                <div class="content_wrapper">
                    <div class="content">
<?php the_content(); ?>
                    </div>
                </div>
<?php if ( comments_open() && ! post_password_required() )  { ?>
                <div class="comments_wrapper">
                    <div class="comments">
<?php comments_template(); ?>
                    </div>
                </div>
<?php } ?>
        </article>
<?php endwhile;
wp_reset_query();
}

So you would then just use the function with the arguments created:

arrr_custom_loop( 'portfolio', 10, 'galleries', 'pirates' );
2

You can get it if you have/know category/subcategory slug...

Just pass it in args array for query_posts.

$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 ,'taxonomy_name'=>'slug of category/subcategory');

query_posts($args); 
1
  • It doesn't seem to work, I do know the slugs, but this shows all the posts within portfolio. I've posted the function that creates the custom post type above if that helps. The categories are galleries and the one I'm trying to pull out is aerial. Commented Jul 16, 2011 at 8:47
0

I've used this before to do more than what you need but if you remove the second taxonomy it'll list all posts in a given category for you:

<?php
                    // set up taxonomies
                    $tax_one = 'project_category';
                    $tax_two = 'brand';
                    $post_type = 'project';

                    $categories = get_categories( array(
                        'type'                     => $post_type,
                        'orderby'                  => 'name',
                        'order'                    => 'ASC',
                        'hide_empty'               => 0,
                        'hierarchical'             => 1,
                        'taxonomy'                 => $tax_one ));

                        foreach ( $categories as $category ) : // list all categories

                            echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>

                                    <?php $terms = get_terms( $tax_two, array( 'hide_empty' => 0 ) );

                                            foreach ( $terms as $term ) :  // list all brands in each category

                                                $myquery['tax_query'] = array(
                                                    array(
                                                        'taxonomy' => $tax_one,
                                                        'terms' => array($category->slug),
                                                        'field' => 'slug',
                                                    ),
                                                    array(
                                                        'taxonomy' => $tax_two,
                                                        'terms' => array($term->slug),
                                                        'field' => 'slug',
                                                    )
                                                        );
                                                $the_posts = new WP_Query($myquery);

                                                if ( $the_posts->have_posts() ) : // if there are posts in the current brand and category then display it

                                                    echo '<li><a href="'.get_term_link( $term->slug, $tax_two ).'">'.$term->name.'</a></li>';

                                                endif;

                                            endforeach; ?>

                            <?php echo '</ul></li>';

                        endforeach; ?>

so tweaking it for your situation:

<?php
                    // set up taxonomies
                    $tax_one = 'category';
                    $post_type = 'portfolio';

                    $categories = get_categories( array(
                        'type'                     => $post_type,
                        'orderby'                  => 'name',
                        'order'                    => 'ASC',
                        'hide_empty'               => 0,
                        'hierarchical'             => 1,
                        'taxonomy'                 => $tax_one ));

                        foreach ( $categories as $category ) : // list all categories

                            echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>

                                    <?php $the_posts = new WP_Query($myquery);

                                                if ( $the_posts->have_posts() ) : // if there are posts in the current category then display it

                                                foreach($the_posts as $post) :

                                                    echo '<li>Your Post stuff here</li>';
                                                endforeach;                                    



                                                endif;

                                            endforeach; ?>

                            <?php echo '</ul></li>';

                        endforeach; ?>

I've not tested this at all but hope it helps!

Cheers,

Dave

1
0

Rajeev's answer worked for me using your code. In a blank install, I did the following:

  1. created a functions.php containing just your portfolio_register function, and the add_action hook that calls it
  2. created two galleries - "aerial" and "not aerial".
  3. created four portfolios - "aerial 1", "aerial 2", "not aerial" and "no gallery", putting them in the galleries the names imply (or none, in the case of "no gallery").
  4. created index.php containing just the first block of code you pasted. The only change I made was to add 'galleries'=>'aerial' to your $args variable:

$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10, 'galleries'=>'aerial' );

Only "aerial 1" and "aerial 2" were displayed.

Any chance your code's not being called (a different template maybe)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.