0
Я создаю пользовательский шаблон страницы, и я не могу заставить страницы постранично работать. Он продолжает показывать те же сообщения, что и на домашней/первой странице. Я пробовал много разных кодов, но получал одинаковые проблемы с каждым результатом.WordPress статический шаблон страницы не работает
Это текущий запрос я использую:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), '');
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Найдено решение, этот код работал:
<?php
if (get_query_var('paged')) {
$paged = get_query_var('paged');
} elseif (get_query_var('page')) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => '2',
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query($custom_query_args);
if ($custom_query->have_posts()) :
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="news-item">
<?php
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), '');
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link('Older Entries', $custom_query->max_num_pages); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link('Newer Entries'); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Спасибо , Я пробовал ваш код и все еще получаю те же 2 результата, которые я получаю на первой/домашней странице. – elw
Убедитесь, что вы установили 2 сообщения в настройках wp-admin>> Чтение> Страницы блога не более –
Я пробовал, но это либо показывает все сообщения, либо когда я использую «post_per_page», разбиение на страницы ничего не делает. – elw