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 have this code to the file category.php :

<?php get_header(); ?>
<div id="content">
<div id="all">
<div id="todo">
<?php 
$my_query = null;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query(array('post_type'=>array('post','articulo'), 'paged' => $paged, 'category_name'=>single_cat_title('', false), 'posts_per_page' => 10));
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); 
if ($my_query->post->post_type == 'post') {?>

<div id="post">
<div id="postimagen"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'thumbnail', $attr ); ?></a></div>
<div id="postcontent">
<div id="categorias">
<?php foreach((get_the_category()) as $category) { 
echo '<li class="cat-' .$category->cat_name. '"><a href="'.get_category_link($category->term_id ).'">' . $category->cat_name . '</a></li>';}?>
</div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><h1><?php the_title(); ?></h1></a>
<div id="info">
<div id="informacion">Escrito por <?php the_author_posts_link(); ?> el <?php the_time('d/m/Y') ?> a las <?php the_time('G:i'); ?></div>
<div id="numcoment"><?php comments_number('0 Comentarios', '1 Comentario', '% Comentarios' );?></div>
</div>
<?php the_excerpt() ?>
<div id="readmore"><a href="<?php the_permalink() ?>">Continuar</a></div>
</div>

<div class="clear"></div>
</div>
<?php } if ($my_query->post->post_type == 'articulo') {?>

<div id="post">
<div class="clear"></div>
<div id="postcontent2"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><h1><?php the_title(); ?></h1></a>
<div id="postmeta">
Escrito por <?php the_author_posts_link(); ?> el <?php the_time('d/m/Y') ?> a las <?php the_time('G:i'); ?>
<div id="numcoment"><?php comments_number('0 Comentarios', '1 Comentario', '% Comentarios' );?></div>
</div>
<?php the_content(false); ?>
<div id="readmore"><a href="<?php the_permalink() ?>">Continuar</a></div>
</div>
<div class="clear"></div>
</div>

<?php } else; endwhile;?>
<div id="navigation">
<div class="previous"><?php previous_posts_link( __( 'Anterior' ) ); ?><div class="clear"></div></div>
<div class="next"><?php next_posts_link( __( 'Siguiente' ) ); ?><div class="clear"></div></div>
</div>
<?php endif; ?>
<?php get_sidebar(); get_footer(); ?>

But the pagination doesn't work in the categories where only there are custom post-type

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The issue is that pagination is based on the main query. Whether or not pages exist is determined by the query that is run before the template is loaded. When you create a new query in the template, it's not connected in any way to that default main query.

The proper way to alter the main query is via the pre_get_posts action:

function wpa_custom_post_types_category( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'articulo' ) );
    }
}
add_action( 'pre_get_posts', 'wpa_custom_post_types_category' );
share|improve this answer
    
I have put this code in functions.php but still not working. hablandoenmanga.com/cat/articulos hablandoenmanga.com/cat/resenas –  seravifer Mar 28 '13 at 21:52
    
I see posts on both of those pages? you should get rid of your custom query and run the normal loop. –  Milo Mar 28 '13 at 22:10
    
Not work: pastebin.com/fmC1ru3E –  seravifer Mar 28 '13 at 22:16
    
With the normal loop, still not working –  seravifer Mar 28 '13 at 22:18
    
if (post_type == 'post') is invalid. enable debugging and fix your php errors. –  Milo Mar 28 '13 at 22:32

Your Answer

 
discard

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

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