0

I have created custom post type called

natural food

In my page-natural-food.php

<div class="main container">
    <div class="content left">
        <?php               
            $type = 'naturalfoods';
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

            $args=array(
              'post_type' => $type,
              'post_status' => 'publish',
              'posts_per_page' => 2,
              'paged'=>$paged            
            );

            $my_query = null;
            $my_query = new WP_Query($args);

            if ( $my_query->have_posts()) :

            while ( $my_query->have_posts()) :  $my_query->the_post(); ?>

            <article class="post">
                <h1>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h1>                   

                <?php the_excerpt(); ?>     

                <div class="readMore right">        
                    <a href="<?php the_permalink(); ?>" class="btnReadMore">Read More&raquo;</a>
                </div><!-- End readMore --> 

                <div class="clear"></div>
            </article>      
            <?php endwhile;

            my_simone_paging_nav(); 

            else :
                echo "<p>No content found</p>";
            endif;

            wp_reset_query();  // Restore global post data stomped by the_post().                   

            ?>                      
    </div><!-- End content -->    
</div><!-- End main -->    
<?php get_footer(); ?>

I am using the below function in index.php

In My functions.php

if ( !function_exists( 'my_simone_paging_nav' ) ) {

    function my_simone_paging_nav() {   

    // Don't print empty markup if there's only one page.
    if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
        return;     
    }

    $paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( '?', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

    $format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    $format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

    // Set up paginated links.
    $links = paginate_links( array(
        'base'     => $pagenum_link,
        'format'   => $format,
        'total'    => $GLOBALS['wp_query']->max_num_pages,
        'current'  => $paged,
        'mid_size' => 2,
        'add_args' => array_map( 'urlencode', $query_args ),
        'prev_text' => __( '&laquo;', 'my-simone' ),
        'next_text' => __( '&raquo;', 'my-simone' ),
        'type'      => 'list',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation right" role="navigation">      
            <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
    }   
}

But pagination not working

Code defined for creating custom post type

function my_custom_posttypes() {

    // Natural Foods Post types
    $labels = array(
        'name'               => 'NaturalFoods',
        'singular_name'      => 'NaturalFoods',
        'menu_name'          => 'NaturalFoods',
        'name_admin_bar'     => 'NaturalFoods',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New NaturalFoods',
        'new_item'           => 'New NaturalFoods',
        'edit_item'          => 'Edit NaturalFoods',
        'view_item'          => 'View NaturalFoods',
        'all_items'          => 'All NaturalFoods',
        'search_items'       => 'Search NaturalFoods',
        'parent_item_colon'  => 'Parent NaturalFoods:',
        'not_found'          => 'No NaturalFoods found.',
        'not_found_in_trash' => 'No NaturalFoods found in Trash.',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_icon'          => 'dashicons-id-alt',
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'naturalfoods' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'comments' ),
        'taxonomies'         => array('category', 'post_tag' )
    );


    register_post_type('naturalfoods', $args);
}

add_action('init','my_custom_posttypes');
  • Can you share your code that defines the custom post type? I guess this will help with identifying the problem... – vlood Dec 26 '14 at 15:40
  • @vlood i updated my question please see – Relax Dec 26 '14 at 15:46
  • Your capability type is okay, and I was thinking that "has_archive" might be messing around... Have you tried creating archive-naturalfoods.php and use paginate_links() there? I'm not really sure that it will work out of archive or search results pages. – vlood Dec 26 '14 at 16:06
0

Change $my_query to $wp_query.
In my_simone_paging_nav(), add the global $wp_query so that get_query_var can find paged.

1

The problem is that your pagination function is looking for page parameter in the global query but you are using a custom query to show your posts. You have two options:

First option: Pass the custom query object to pagination function:

In the pagination option, instead of using the global query object, use the custom query passed as argument:

function my_simone_paging_nav( $query ) {   

    // Don't use the global wp_query
    //if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
    if( $query->max_num_pages < 2 ) {
        return;     
    }

Second option: use the archive template and leave WordPress handle the query and pages:

You have created a page template (page-natural-food.php) and then you are using that page template as custom post type archive. That mean that WordPress makes the query for that page and then in the page you have to build a custom query to show what you want. Instead, you could use a custom post type archive template and WordPress will make the query for that without the need of a custom extra query. You can use the generic archive.php template or, if you need a specific template only for that custom post type use the archive-natural-food.php (assuming that the custom post type slug is natural-food). The content of archive-natural-food.php could just like this:

<div class="main container">
    <div class="content left">

        if ( have_posts()) :

        while (have_posts() ) {
            the_post();
            ?>

            <article class="post">
                <h1>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h1>                   

                <?php the_excerpt(); ?>     

                <div class="readMore right">        
                    <a href="<?php the_permalink(); ?>" class="btnReadMore">Read More&raquo;</a>
                </div><!-- End readMore --> 

                <div class="clear"></div>
            </article>      
            <?php
            }

        my_simone_paging_nav(); 

        } else {
            echo "<p>No content found</p>";
        }          

        ?>                      
    </div><!-- End content -->    
</div><!-- End main -->    
<?php get_footer(); ?>
  • Much better answer. – shanebp Dec 31 '14 at 15:02
  • Really? I don't think so. I provide full explanation of what was going wrong and provide different ways to solve the problem. I suggest a way to avoid the use the global $wp_query, which should be avoid for secondary loops and something that is being abandoned in PHP programming in general; also I suggest the use of archive template which is the best option in this situaion with no doubt. Anyway, the OP choose the answer, even it is not the best. – cybmeta Dec 31 '14 at 16:20
  • Sorry @shanebp I think I didnt' understand your comment at first read. At second read I think you agree that this answer is better. – cybmeta Dec 31 '14 at 16:32
  • Exactly. Full stipulation: The answer given by cybermeta is much better than the answer given by shanebp. – shanebp Dec 31 '14 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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