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];
?>

4 Answers
4

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="https://wordpress.stackexchange.com/questions/23136/<?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' );

Leave a Reply

Your email address will not be published. Required fields are marked *