Skip to content

Instantly share code, notes, and snippets.

@sectsect
Last active March 20, 2016 19:27
Show Gist options
  • Save sectsect/e3b91cef0c2415bb8c49 to your computer and use it in GitHub Desktop.
Save sectsect/e3b91cef0c2415bb8c49 to your computer and use it in GitHub Desktop.
Wordpress: Split the single page for each array without "<!--nextpage-->" on the template. Supported the preview page.
<?php
function is_single_paged($page){
global $wp_query;
if(!is_preview()){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
}else{
$pagenum = $_GET['paged'];
$paged = ($pagenum) ? $pagenum : 1;
$wp_query->query = array('p' => $post->ID, 'page' => $paged, 'preview' => true, 'post_type' => get_post_type() );
$wp_query->set('paged', $paged);
}
if(is_single() && $paged == $page){
return true;
}else{
return false;
}
}
?>
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article>
<h1><?php the_title(); ?></h1>
<?php if (is_single_paged(1)): ?>
<section>
something...
</section>
<?php endif; ?>
<?php
$fields = CFS()->get('section');
$fields = array_chunk($fields, 2);
if(!is_preview()){
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
}else{
$pagenum = $_GET['paged'];
$paged = ($pagenum) ? $pagenum : 1;
}
$key = $paged - 1; // "-1" For Array's key
$pagecount = count($fields);
$fields = $fields[$key];
foreach ($fields as $field):
?>
<section>
<?php if ($field['h2']): ?>
<h2><?php echo $field['h2']; ?></h2>
<?php endif; ?>
<?php if ($field['h3']): ?>
<h3><?php echo $field['h3']; ?></h3>
<?php endif; ?>
<?php if ($field['text']): ?>
<div class="wp-editor">
<?php echo $field['text']; ?>
</div>
<?php endif; ?>
</section>
<?php endforeach; ?>
<section class="pager">
<?php
if(!is_preview()){
$args1 = array(
'base' => get_the_permalink() . '%#%/',
'format' => get_the_permalink() . '%#%/'
);
}else{
$args1 = array(
'base' => get_the_permalink() . '&paged=%#%',
'format' => get_the_permalink() . '&paged=%#%'
);
}
$args2 = array(
'total' => $pagecount,
'current' => $paged,
'show_all' => false,
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __('&laquo; Previous'),
'next_text' => __('Next &raquo;'),
'type' => 'list',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
$args = array_merge($args1, $args2);
echo paginate_links($args);
?>
</section>
</article>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment