0

I use CPT UI to create custom taxonomy. It's code i got :

add_action( 'init', 'cptui_register_my_taxes' );
function cptui_register_my_taxes() {

    $labels = array(
        "name" => "Product Categories",
        "label" => "Product Categories",
        "menu_name" => "Product Categories",
        "all_items" => "All product categories",
        "edit_item" => "Edit product category",
        "view_item" => "View product category",
        "update_item" => "Update product category",
        "add_new_item" => "Add new product category",
        "new_item_name" => "New Product Category",
        "parent_item" => "Parent Product Category",
        "parent_item_colon" => "Parent Product Category:",
        "search_items" => "Search Product Categories",
        "popular_items" => "Popular Product Categories",
        "separate_items_with_commas" => "Separate product categories with commas",
        "add_or_remove_items" => "Add or remove Product Categories",
        "choose_from_most_used" => "Chose from the most used Product Categories",
        "not_found" => "No Product Categories found",
    );

    $args = array(
        "labels" => $labels,
        "hierarchical" => true,
        "label" => "Product Categories",
        "show_ui" => true,
        "query_var" => true,
        "rewrite" => false,
        "show_admin_column" => true,
    );
    register_taxonomy( "product-category", array( "product" ), $args );
}

And my query:

function the_product(){
    $slug = get_query_var('term');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'product',
        'tax_query' => array(
            array(
                'taxonomy' => 'product-category',
                'field'    => 'slug',
                'terms'    => $slug,
            ),
        ),
        'posts_per_page' => 6,
        'paged'=>$paged,
    );
    $query = new WP_Query( $args );
    return $query;
}

It's my Paginate:

function pagination($query,$cat_Url) {
    $page = $query->query_vars["paged"];
    if ( !$page ) $page = 1;
    $qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";
    // Only necessary if there's more posts than posts-per-page
    if ( $query->found_posts > $query->query_vars["posts_per_page"] ) {
        echo '<div class="paging-wrap">';
        // Previous link?
        if ( $page > 1 ) {
            echo '<a href="'.get_site_url().$cat_Url.'/page/'.($page-1).'/'.$qs.'"><p class="next-posts"><span class="glyphicon glyphicon-chevron-left"></span> <span>Previous</span></p></a>';
        }
        // Loop through pages
        for ( $i=1; $i <= $query->max_num_pages; $i++ ) {
            // Current page or linked page?
            if ( $i == $page ) {
                echo '<span class="pag-counter current">'.$i.'</span>';
            } else {
                echo '<a href="'.get_site_url().$cat_Url.'/page/'.$i.'/'.$qs.'"><span class="pag-counter">'.$i.'</span></a>';
            }
        }
        // Next link?
        if ( $page < $query->max_num_pages ) {
            echo '<a href="'.get_site_url().$cat_Url.'/page/'.($page+1).'/'.$qs.'"> <p class="prev-posts"><span>Next</span> <span class="glyphicon glyphicon-chevron-right"></span></p></a>';
        }
        echo '<div class="clearfix"></div>';
        echo '</div>';
    }
}

And i call in file taxonomy-product-category.php

<div class="products">
            <?php $article = the_product(); ?>
            <?php if($article) : while($article->have_posts()) : $article->the_post(); ?>
                <div class="product col-md-4 col-sm-6 col-xs-12 img-wrap">
                    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('square-small') ?></a>
                    <div class="product-detail">
                        <h4><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h4>
                        <p>
                            <?php if(get_field('sale_price')!=''){ ?>
                                <span class="price"><?php zo_money(get_field('sale_price')) ?></span>
                                <span class="old-price"><?php zo_money(get_field('price')) ?></span>
                            <?php }else{ ?>
                                <span class="price"><?php zo_money(get_field('price')) ?></span>
                            <?php } ?>
                        </p>
                    </div>
                    <div class="stocking">
                        <?php if(is_new()){ ?><p><span class="new">NEW</span></p><?php } ?>
                        <?php if(is_sale()){ ?><p><span class="sale">SALE</span></p><?php } ?>
                    </div>
                </div>
            <?php endwhile; endif; wp_reset_postdata(); ?>
            <div class="clearfix"></div>
        </div>
        <div class="paging">
            <?php $cur_term = '/product-category/'.get_queried_object()->slug; ?>
            <?php  pagination($article,$cur_term); ?>
        </div>
    </div>

When I click to page 2 : my-site/product-category/my-customcategory/page/2/ Then result 404 error . Pls help me ! Thanks for any answer.

  • How are you generating the paginate links? And please, don't roll back my edition. I'm trying to make the code more easy to read. – cybmeta Nov 13 '15 at 8:33
  • Sorry , my bad ! Just edit for best looking . I updated my paginate link – Andy Nguyen Nov 13 '15 at 8:43
  • Whether or not a page exists is based on the results of the main query run by WordPress before the template is loaded. Why are you creating your own query in the template? If you need to change any query parameters, use pre_get_posts. – Milo Nov 13 '15 at 16:24

Your Answer

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

Browse other questions tagged or ask your own question.