0

I'm having trouble and got stuck on trying to implant pagination inside my custom post type. I've done research from here with little to no luck, I've surfed the already answered questions here, yet I didnt seem to find anything that I could try out in my case. I've checked some tutorials on youtube and got something small started but far from what I actually want :/

First off I'll paste the taxonomy

    function porocne_obleke_custom_taxonomies() {
    
    //add new taxonomy hierarchical
    $labels = array(
        'name' => 'Obleke',
        'singular_name' => 'Obleke',
        'search_items' => 'Search Obleke',
        'all_items' => 'All Obleke',
        'parent_item' => 'Parent Obleke',
        'parent_item_colon' => 'Parent Obleke',
        'edit_item' => 'Edit Obleke',
        'update_item' => 'Update Obleke',
        'add_new_item' => 'Dodaj Nove Obleke',
        'new_item_name' => 'Dodaj Nov Ime Za Obleke',
        'menu_name' => 'Dodajanje Oblek',
        );
    
    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'porocne_obleke' ),
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'revision',
            'custom-fields',
            'page-attributes',
            'post-formats',),
    );
    
    register_taxonomy('porocne_obleke', array('obleke'), $args);
}

After I created the Custom Post Type where I added Pagination on the end of the string that I'm presenting inside the website - it is outside the while loop and after it; BUT when I'm trying it on the website you can see that the "<- previous" is above the picture (I want it under if possible) and its function doesn't work (I currently have 6 posts ready), I set the 'posts_per_page'=> 4 so only 4 are displayed, yet I can not reach the other 2 with pagination function - due to me not knowing how to implement it most probably...

function ultimate_shortcode($atts) {

    $atts = shortcode_atts( array(
        'porocne_obleke' => ''
    ), $atts );

  //If category is multiple: 8,9,3
  $categories  = explode(',' , $atts['porocne_obleke']);

    $args = array(
            // 'post_type'     => 'porocne_obleke',
            'post_type'     => 'obleke', //obleke, ne porocne_obleke za post type
            'post_status'   => 'publish',
            'posts_per_page'=> 4,
            'tax_query'     => array( array(
                                'taxonomy'  => 'porocne_obleke',
                                'field'     => 'term_id',
                                'terms'     => $categories
                            ) )
        );

        $string = '';
        $query = new WP_Query( $args );

        if( ! $query->have_posts() ) {
            return false;
        }

        while( $query->have_posts() ){
            $query->the_post();
            $string .= '<div class="row">
                            <div class="column">
                            <th>
                                <span class="track_img">'. get_the_post_thumbnail().'</span>
                                <span class="track_content">'.apply_filters('the_content',get_the_content()).'</span>
                            </th>
                            </div>
                        </div>';
        }
        wp_reset_postdata();
        
        
        
        if ( $query->max_num_pages > 1 && is_page()) {
            $string .= '<nav class="prev-next-post">';
            $string .= '<div class="nav-previous text-left">';
            $string .= get_next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous'), $query->max_num_pages );
            $string .= '</div>';
            $string .= '<div class="nav-previous text-right">';
            $string .= get_previous_posts_link( __( '<span class="meta-nav">&rarr;</span> Next'));
            $string .= '</div>';
            $string .= '</nav>';
            
        }
        
        return $string;
        
}

Is there anyway you could help out? I'm working inside Child theme, so there is only function.php file inside.

Thanks alot for taking your time!

6
  • Is there a reason you're using a shortcode and not the automatically generated archive that will be generated at /porocne_obleke? Pagination will work automatically on the default archive, and isn't really designed to be used with secondary queries like this. Feb 2, 2018 at 14:49
  • Well the reason is, there are 3 sites that I'm using same "gallery" I'm just calling different category IDs - in which I put the custom post types
    – Pr0metheus
    Feb 2, 2018 at 14:52
  • So this is for an embeddable gallery that needs to be paginated? Feb 2, 2018 at 14:55
  • 1
    See the accepted answer to this similar question: wordpress.stackexchange.com/questions/120407/… Feb 2, 2018 at 14:59
  • 1
    Was about to post the same. @JacobPeattie Shamelessly promoting my answer to How to paginate attachments in a secondary query as gallery?, but there definitely is an argument for doing this with ajax anyway. Feb 2, 2018 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.