Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been searching the web for a long time now, but couldn't find a solid simple anwser to, what seems to be a basic question: I have to output a list of custom post type categories and the posts of the first category on index page. Each category of the list should link to its page, showing its posts, and each post should go to the single page. I have a custom post type and its custom taxonomy registered and it seems to work.

This is on functions.php:

<?php
function my_custom_post_empreendimento() {
$labels = array(
    'name'               => _x( 'Empreendimentos', 'post type general name' ),
    'singular_name'      => _x( 'Empreendimento', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Empreendimento' ),
    'edit_item'          => __( 'Edit Empreendimento' ),
    'new_item'           => __( 'New Empreendimento' ),
    'all_items'          => __( 'All Empreendimentos' ),
    'view_item'          => __( 'View Empreendimento' ),
    'search_items'       => __( 'Search Empreendimentos' ),
    'not_found'          => __( 'No empreendimentos found' ),
    'not_found_in_trash' => __( 'No empreendimentos found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Empreendimentos'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our empreendimentos specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
    'has_archive'   => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'can_export' => true
);
register_post_type( 'empreendimento', $args );  
}
add_action( 'init', 'my_custom_post_empreendimento' );

function my_taxonomies_empreendimento() {
$labels = array(
    'name'              => _x( 'Empreendimento Categories', 'taxonomy general    name' ),
    'singular_name'     => _x( 'Empreendimento Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Empreendimento Categories' ),
    'all_items'         => __( 'All Empreendimento Categories' ),
    'parent_item'       => __( 'Parent Empreendimento Category' ),
    'parent_item_colon' => __( 'Parent Empreendimento Category:' ),
    'edit_item'         => __( 'Edit Empreendimento Category' ), 
    'update_item'       => __( 'Update Empreendimento Category' ),
    'add_new_item'      => __( 'Add New Empreendimento Category' ),
    'new_item_name'     => __( 'New Empreendimento Category' ),
    'menu_name'         => __( 'Empreendimento Categories' )
);
$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'label' => 'Empreendimentos',  
    'query_var' => true,  
    'rewrite' => array('slug' => 'empreendimentos')
);
register_taxonomy( 'empreendimento_category', 'empreendimento', $args );
}
add_action( 'init', 'my_taxonomies_empreendimento', 0 );
?>

But the linking-to-which-template part isn't clear.

This is on index.php:

<?php get_header(); ?>
<div class="empgalist">
<?php $post_type = 'empreendimento';
$tax = 'empreendimento_category';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms  as $tax_term) {
$argslist=array(
  'post_type' => $post_type,
  '$tax' => $tax_term->slug,
  'post_status' => 'publish'
);
$my_query = null; 
$my_query = new WP_Query($argslist); 
if( $my_query->have_posts() ) { ?>
  <div class="empgalistitem">
  <?php echo '<li><a href="'.get_term_link($tax_term->slug, $tax).'">'.$tax_term->name.'</a></li>'; ?>
  </div><!-- empgalistitem-end -->
  <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <!-- <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>" rel="bookmark" target="_blank"><?php the_title(); ?></a></p> -->
    <?php endwhile;
} 
wp_reset_query();   
}
}
?>
</div><!-- empgalist-end -->
<div class="empgalwrap">
<div class="empgal">
<?php
$args = array(
    'post_type' => 'empreendimento',
    'tax_query' => array(
        array(
            'taxonomy' => 'empreendimento_category',
            'field' => 'slug',
            'terms' => 'comerciais',
            'order' => 'ASC'
        )
    )
);?>
<div class="gallery gallery1">
<div class="holder"><ul>
<?php $empreendimentos = new WP_Query( $args );
if( $empreendimentos->have_posts() ) {
    while( $empreendimentos->have_posts() ) {
        $empreendimentos->the_post();
        ?>
        <li>
            <div class="galpost">
            <div class="galcell">
            <div id="areatit" class="entry-title"><h1><?php the_title(); ?></h1></div>
            <div class="end">
            <?php $endereco = get_post_meta($post->ID, 'Endere├žo', true); ?>
            <?php echo $endereco; ?>
            </div>
            <div class="status">
            <?php $status = get_post_meta($post->ID, 'Status', true); ?>
            <?php echo $status; ?>
            </div>
            </div><!-- galcell-end -->
            </div><!-- galpost-end -->
            </li>
        <?php
    }
}
else {
    echo 'Oh ohm no empreendimentos!';
}
wp_reset_query();
?>  
<div class="clearfloat"></div>
</ul>
</div>
<div class="control">
<a class="prev" href="#">prev</a>
<a class="next" href="#">next</a>
</div>
</div>
</div><!-- empgal-end -->
</div><!-- empgalwrap-end -->
<?php get_footer(); ?>

Guess I don't need two queries to do this but I couldn't figure how to merge them. Than I have a page to which I associate a template with the same code above, but the links don't work.

Any thoughts? Thank you in advance.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.