I'm at the end of my tether now. Really need some help! Here's the setup...
I have 2 custom post types in my functions.php:
// Register the events post type
function events() {
$labels = array(
'name' => _x('Evenemang', ''),
'singular_name' => _x('Evenemang', ''),
'menu_name' => _x('Evenemang', ''),
'name_admin_bar' => _x('Evenemang', ''),
'add_new' => _x('Skapa ny', 'evenemang'),
'add_new_item' => __('Skapa nyt evenemang'),
'search_items' => __('Sök evenemang'),
'not_found' => __('Hittade inga evenemang.'),
'not_found_in_trash' => __('Inga evenemang hittades i papperskorgen.'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_in_menu' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'evenemang'),
'query_var' => 'evenemang',
'menu_icon' => 'dashicons-calendar-alt',
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('evenemang', $args);
}
add_action('init', 'events');
// Register the recipes post type
function recipes() {
$labels = array(
'name' => _x('Recept', ''),
'singular_name' => _x('Recept', ''),
'menu_name' => _x('Recept', ''),
'name_admin_bar' => _x('Recept', ''),
'add_new' => _x('Skapa ny', 'recept'),
'add_new_item' => __('Skapa ny recept'),
'search_items' => __('Sök recept'),
'not_found' => __('Hittade inga recept.'),
'not_found_in_trash' => __('Inga recept hittades i papperskorgen.'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_in_menu' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'recept'),
'query_var' => 'recept',
'menu_icon' => 'dashicons-carrot',
'supports' => array('title', 'thumbnail')
);
register_post_type('recept', $args);
}
add_action('init', 'recipes');
For each of these 2 I have an archive-{post_type}.php, and i'm using <?php echo paginate_links(); ?>
to spit out the pagination links, here's archive-evenemang.php:
<?php get_header(); ?>
<div class="list">
<?php
$today = date('Ymd');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 8,
'post_type' => 'evenemang',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'event_date',
'compare' => '>=',
'value' => $today
)
)
);
?>
<?php $posts = get_posts($args); foreach ($posts as $post) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<?php endforeach; wp_reset_postdata(); ?>
</div>
<div class="pagination">
<?php echo paginate_links(); ?>
</div>
<?php get_footer(); ?>
and here's archive-recept.php:
<?php get_header(); ?>
<div class="list">
<?php
$today = date('Ymd');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 8,
'post_type' => 'recept',
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged
);
?>
<?php $posts = get_posts($args); foreach ($posts as $post) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<?php endforeach; wp_reset_postdata(); ?>
</div>
<div class="pagination">
<?php echo paginate_links(); ?>
</div>
<?php get_footer(); ?>
As you can see they are set up exactly the same in functions.php, and use the basically the same archive template (with a minor adjustment to the $args in the query).
The "evenemang" page works perfectly, it shows the results, the pagination buttons and i can access mydomain.com/evenemang/page/2/.
The "recept" page also works, it shows the results, BUT no pagination buttons and if i try and access mydomain.com/recept/page/2/ it just goes to the front page.
Why does it work for one and not the other? Do i need to change some args inside the paginate_links()
function? A problem with using get_query_var('paged')
on multiple archives? Is it a .htaccess problem?