3
votes

I'm a novice and I'm trying to develop my first theme.

Unfortunately I'm experiencing some troubles with custom post type (projet). I have tried different combinations found about this topics (query_post, WP_Query, flush permalinks, etc.), but none seems to work for me.

I try to use the plugin wp_pagenavi.

Here is my register post type, in function.php :

register_post_type('projet', array(
    'label' => __('Réalisations'),
    'singular_label' => __('Projet'),
    'public' => true,
    'show_ui' => true,
    'rewrite' => array('slug' => 'international/realisations', 'with_front' => true),
    'hierarchical' => false,
    'has-archive' => true,
    'query_var' => true,
    'supports' => array('title', 'excerpt', 'editor', 'thumbnail', 'page-attributes')
));

Here is the code from the template that list my custom post type "projet":

 <ul id="list-projets" class="grid cs-style-7">
        <?php if ( get_query_var('paged') ) {
            $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
            $paged = get_query_var('page');
        } else {
            $paged = 1;
        } ?>
            <?php
            $args= array(
                        'showposts' => 2,
                        'posts_per_page' => 2,
                        'post_type'  => 'projet',
                        'paged' => $paged
                        );
            $the_query = new WP_Query($args);

            if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post(); ?> 

              <li>
                    <figure>
                        <?php the_post_thumbnail(); ?>
                        <h3><?php the_title(); ?></h3>
                        <figcaption>
                            <span><?php echo get_the_excerpt(); ?></span>
                            <a href="<?php the_permalink(); ?>">+ de détails</a>
                        </figcaption>
                    </figure>
                </li>

            <?php endwhile; ?>    
            <?php endif; 
            wp_reset_query(); ?>
            <nav>
                <?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
            </nav>
    </ul>

Currently this code lists the 2 last custom post type "projet" and the pagination, but if I click on the page 2 it displays a 404 error. The url is /localhost/wordpress/international/realisations/page/2/

I tried to add 'has_archive' => true to my register post type, but when I save and flush the permalinks and return to my listing page /localhost/wordpress/international/realisations/ the page is empty and my breadcumb is no more good "ACCUEIL > RÉALISATIONS" instead of "ACCUEIL > INTERNATIONAL > RÉALISATIONS".

I'm a novice so maybe I did something wrong, please help me because I've been searching for a few days and I don't know what to do !

I also tested this solution (still 404), at this link.

1
Hi Everyone, finally i found a solution that works for me : somadesign.ca/projects/smarter-custom-post-typesTiergAu
This will not work until you rewrite the slug for your custom taxonomies. I had the same problem and gave an answer here on StackOverflow:jewelfarazi

1 Answers

1
votes

Insted of this

<nav>
    <?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
</nav>

Use this

<div class="pagination">
<?php             
    global $wp_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
?>
</div>