My site is http://visualdesignworkshop.com/
I want pagination on the bottom of my news page 1,2,3,4,5 and alse be able to use older post/newer post. This my custom code, i'm a beginner in php so keep that in mind :)
index.php exact the same as home.php
<?php
/*
Template name: Home Page
*/
?>
<?php get_header(); ?>
<div id="sliderfotocontainer"><!-- START MAIN FOTO CONTAINER -->
<div class="sliderpwrap" class="group"> <!-- START WRAPPER -->
<?php echo get_royalslider(1); ?>
</div> <!-- END WRAPPER -->
</div><!-- END SLIDER FOTO CONTAINER -->
<body<?php body_class(); ?>>
<div class="pwrap" class="group"> <!-- START WRAPPER -->
<div class="main_container"><!--START MAIN_CONTAINER -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 5,'cat' => 18, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>News</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN CONTENT -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 1,'cat' => 27, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>Events</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN CONTENT -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 1,'cat' => 17, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>Interviews</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN_CONTENT -->
</div> <!-- END MAIN_CONTAINER -->
<?php get_sidebar(); ?>
</div> <!-- END WRAPPER -->
<?php get_footer(); ?>
Single.php
<?php
/*
Template name: News Page
*/
?>
<?php get_header(); ?>
<div id="sliderfotocontainernews"><!-- START MAIN FOTO CONTAINER -->
<div class="sliderpwrap" class="group"> <!-- START WRAPPER -->
<?php echo get_royalslider(2); ?>
</div> <!-- END WRAPPER -->
</div><!-- END SLIDER FOTO CONTAINER -->
<div class="pwrap" class="group"> <!-- START WRAPPER -->
<div class="main_container"><!--START MAIN_CONTAINER -->
<h1 class="onthespot">On the spot</h1>
<div class='main_content'>
<?php query_posts ("post_per_page=20"); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>News</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN_CONTENT -->
</div><!--END MAIN_CONTAINER -->
<?php get_sidebar(); ?>
</div> <!-- END WRAPPER -->
<?php get_footer(); ?>
The function code looks like this
<?php
// Load jQuery
if ( !is_admin() ) {
wp_deregister_script('jquery');
wp_register_script('jquery', get_bloginfo('template_directory') ."/js/jquery-1.7.1.min.js");
wp_enqueue_script('jquery');
}
// Clean up the <head>
function removeHeadLinks() {
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
}
add_action('init', 'removeHeadLinks');
remove_action('wp_head', 'wp_generator');
// Declare sidebar widget zone
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Sidebar Widgets',
'id' => 'sidebar-widgets',
'description' => 'These are widgets for the sidebar.',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
// Register Dynamic Menu
if (function_exists('register_nav_menus')) {
register_nav_menus(
array(
'main_nav' => 'Main Navigation Menu'
)
);
}
// Excerpt Readmore Link
function excerpt_readmore($more) {
return '... <a href="'. get_permalink($post->ID) . '" class="readmore">' . 'Read More' . '</a>';
}
add_filter('excerpt_more', 'excerpt_readmore');
?>
I think this is surely a easy dance in the sun with tequila sunrise in your hand for you. So i want also to add previous and next article in the single page as you can see in the a must see short film single page in the left corner below the movie.
That's it for now, thank you.
Michael Caputo answers:
Add this to your functions.php
/***** Numbered Page Navigation (Pagination) Code.
Tested up to WordPress version 3.1.2 *****/
/* Function that Rounds To The Nearest Value.
Needed for the pagenavi() function */
function round_num($num, $to_nearest) {
/*Round fractions down (http://php.net/manual/en/function.floor.php)*/
return floor($num/$to_nearest)*$to_nearest;
}
/* Function that performs a Boxed Style Numbered Pagination (also called Page Navigation).
Function is largely based on Version 2.4 of the WP-PageNavi plugin */
function pagenavi($before = '', $after = '') {
global $wpdb, $wp_query;
$pagenavi_options = array();
$pagenavi_options['pages_text'] = ('Page %CURRENT_PAGE% of %TOTAL_PAGES%:');
$pagenavi_options['current_text'] = '%PAGE_NUMBER%';
$pagenavi_options['page_text'] = '%PAGE_NUMBER%';
$pagenavi_options['first_text'] = ('First Page');
$pagenavi_options['last_text'] = ('Last Page');
$pagenavi_options['next_text'] = '»';
$pagenavi_options['prev_text'] = '«';
$pagenavi_options['dotright_text'] = '...';
$pagenavi_options['dotleft_text'] = '...';
$pagenavi_options['num_pages'] = 10; //continuous block of page numbers
$pagenavi_options['always_show'] = 0;
$pagenavi_options['num_larger_page_numbers'] = 0;
$pagenavi_options['larger_page_numbers_multiple'] = 5;
//If NOT a single Post is being displayed
/*http://codex.wordpress.org/Function_Reference/is_single)*/
if (!is_single()) {
$request = $wp_query->request;
//intval — Get the integer value of a variable
/*http://php.net/manual/en/function.intval.php*/
$posts_per_page = intval(get_query_var('posts_per_page'));
//Retrieve variable in the WP_Query class.
/*http://codex.wordpress.org/Function_Reference/get_query_var*/
$paged = intval(get_query_var('paged'));
$numposts = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;
//empty — Determine whether a variable is empty
/*http://php.net/manual/en/function.empty.php*/
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = intval($pagenavi_options['num_pages']);
$larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
$larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
$pages_to_show_minus_1 = $pages_to_show - 1;
$half_page_start = floor($pages_to_show_minus_1/2);
//ceil — Round fractions up (http://us2.php.net/manual/en/function.ceil.php)
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $paged - $half_page_start;
if($start_page <= 0) {
$start_page = 1;
}
$end_page = $paged + $half_page_end;
if(($end_page - $start_page) != $pages_to_show_minus_1) {
$end_page = $start_page + $pages_to_show_minus_1;
}
if($end_page > $max_page) {
$start_page = $max_page - $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) {
$start_page = 1;
}
$larger_per_page = $larger_page_to_show*$larger_page_multiple;
//round_num() custom function - Rounds To The Nearest Value.
$larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page;
$larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple;
$larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple;
$larger_end_page_end = round_num($end_page, 10) + ($larger_per_page);
if($larger_start_page_end - $larger_page_multiple == $start_page) {
$larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
$larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
}
if($larger_start_page_start <= 0) {
$larger_start_page_start = $larger_page_multiple;
}
if($larger_start_page_end > $max_page) {
$larger_start_page_end = $max_page;
}
if($larger_end_page_end > $max_page) {
$larger_end_page_end = $max_page;
}
if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
/*http://php.net/manual/en/function.str-replace.php */
/*number_format_i18n(): Converts integer number to format based on locale (wp-includes/functions.php*/
$pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
$pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
echo $before.'<div class="row"><ul class="pagination twelve columns">'."\n";
//Displays a link to the previous post which exists in chronological order from the current post.
/*http://codex.wordpress.org/Function_Reference/previous_post_link*/
echo '<li>';
previous_posts_link($pagenavi_options['prev_text']);
echo '</li>';
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
//esc_url(): Encodes < > & " ' (less than, greater than, ampersand, double quote, single quote).
/*http://codex.wordpress.org/Data_Validation*/
//get_pagenum_link():(wp-includes/link-template.php)-Retrieve get links for page numbers.
echo '<li><a href="'.esc_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">1</a></li>';
if(!empty($pagenavi_options['dotleft_text'])) {
echo '<li class="expand">'.$pagenavi_options['dotleft_text'].'</li>';
}
}
if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
}
}
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
$current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
echo '<li class="current">'.$current_page_text.'</li>';
} else {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
}
}
if ($end_page < $max_page) {
if(!empty($pagenavi_options['dotright_text'])) {
echo '<li class="expand">'.$pagenavi_options['dotright_text'].'</li>';
}
$last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
echo '<li><a href="'.esc_url(get_pagenum_link($max_page)).'" class="last" title="'.$last_page_text.'">'.$max_page.'</a></li>';
}
echo '<li>';
next_posts_link($pagenavi_options['next_text'], $max_page);
echo '</li>';
if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
$page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
echo '<li><a href="'.esc_url(get_pagenum_link($i)).'" class="single_page" title="'.$page_text.'">'.$page_text.'</a></li>';
}
}
echo '</ul></div>'.$after."\n";
}
}
}
Then this after "Endwhile" in your index php:
<?php if(function_exists('pagenavi')) { pagenavi(); } else { ?>
<div class="post postNavigation">
<div class="floatLeft"><?php next_posts_link('« Older Entries') ?></div>
<div class="floatRight"><?php previous_posts_link('Newer Entries »') ?></div>
<div class="clearMe"></div>
</div>
<?php } ?>
Michael Caputo comments:
Here's a bit of style to put in your style.css file as well:
ul.pagination{display:block;height:24px;margin-left:-5px}
ul.pagination li{float:left;display:block;height:24px;color:#999;font-size:14px;margin-left:5px}
ul.pagination li a{display:block;padding:1px 7px 1px;color:#555}
ul.pagination li:hover a,ul.pagination li a:focus{background:#e6e6e6}
ul.pagination li.unavailable a{cursor:default;color:#999}
ul.pagination li.unavailable:hover a,ul.pagination li.unavailable a:focus{background:transparent}
ul.pagination li.current a{background:#056597;color:white;font-weight:bold;cursor:default}
ul.pagination li.current a:hover{background:#056597}
Veritus comments:
Nothing happens :S
Michael Caputo comments:
Absolutely nothing happens?
Did you add it to both your home.php and index.php?
Veritus comments:
yes, i'm waiting for the webhost to end their maintenance, i have problems uploading files again, i overwrote files before but... Time is now 03:16, time to sleep.
Veritus comments:
nope, nothing happens..
Michael Caputo comments:
That's not possible.
It should display the left and right links if the pagenavi plugin isn't there. Look at the conditional i provided to be added to the index and home php files (Are you sure you did that part?)
<div class="post postNavigation">
<div class="floatLeft"><?php next_posts_link('« Older Entries') ?></div>
<div class="floatRight"><?php previous_posts_link('Newer Entries »') ?></div>
<div class="clearMe"></div>
</div>
Can you do that, and post your index and home php files after that?
Veritus comments:
oki, i have the pagenavi plugin installed for my the a must see short film page.. :S
Michael Caputo comments:
That isn't the plugin I gave you to install there.
I can tell from the HTML on that page.
Michael Caputo comments:
It doesn't sound like you are editing the right files.
if you want to provide me with FTP access I can probably fix this.
Veritus comments:
Home.php
<?php
/*
Template name: Home Page
*/
?>
<?php get_header(); ?>
<div id="sliderfotocontainer"><!-- START MAIN FOTO CONTAINER -->
<div class="sliderpwrap" class="group"> <!-- START WRAPPER -->
<?php echo get_royalslider(1); ?>
</div> <!-- END WRAPPER -->
</div><!-- END SLIDER FOTO CONTAINER -->
<body<?php body_class(); ?>>
<div class="pwrap" class="group"> <!-- START WRAPPER -->
<div class="main_container"><!--START MAIN_CONTAINER -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 5,'cat' => 18, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>News</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php if(function_exists('pagenavi')) { pagenavi(); } else { ?>
<div class="post postNavigation">
<div class="floatLeft"><?php next_posts_link('« Older Entries') ?></div>
<div class="floatRight"><?php previous_posts_link('Newer Entries »') ?></div>
<div class="clearMe"></div>
</div>
<?php } ?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN CONTENT -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 1,'cat' => 27, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>Events</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php if(function_exists('pagenavi')) { pagenavi(); } else { ?>
<div class="post postNavigation">
<div class="floatLeft"><?php next_posts_link('« Older Entries') ?></div>
<div class="floatRight"><?php previous_posts_link('Newer Entries »') ?></div>
<div class="clearMe"></div>
</div>
<?php } ?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN CONTENT -->
<div class="main_content">
<?php query_posts ( array( 'posts_per_page' => 1,'cat' => 17, 'paged' => get_query_var('page') ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1>Interviews</h1>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
<a href="<?php the_permalink(); ?>">
<img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" /></a>
<?php the_excerpt(); ?>
<?php endwhile;?>
<?php if(function_exists('pagenavi')) { pagenavi(); } else { ?>
<div class="post postNavigation">
<div class="floatLeft"><?php next_posts_link('« Older Entries') ?></div>
<div class="floatRight"><?php previous_posts_link('Newer Entries »') ?></div>
<div class="clearMe"></div>
</div>
<?php } ?>
<?php else : ?>
<p>Searching and there isn't there!</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- END MAIN_CONTENT -->
</div> <!-- END MAIN_CONTAINER -->
<?php get_sidebar(); ?>
</div> <!-- END WRAPPER -->
<?php get_footer(); ?>
Veritus comments:
oki, i'll give the access..this is getting me nightmares :)
Veritus comments:
ftp.[XXXXXXXXXXXXXXXX]
User name: [XXXXXXXXXX]
password: [XXXXXXXXXX]
Michael Caputo comments:
REMOVE YOUR FTP INFORMATION< this is a public website.
Veritus comments:
visualdesignworkshop/public_html/wp-content/themes/vdw thats the folder for the main page, which is the news page.
Veritus comments:
i seems i can't edit, but i changed the password..where can i reach you for the pass?
Michael Caputo comments:
I sent my email address [email protected] to you, or skype: flashpunkca
Michael Caputo comments:
Got the info I need.
It appears that none of the changes i'm saving are showing up on the live site.
Do you have a caching plugin enabled?
Veritus comments:
yes, i have..should i turn in off?
Michael Caputo comments:
yes please, for now at least
Veritus comments:
i added you on skype :)
John Cotton answers:
Have you looked at [[LINK href="http://codex.wordpress.org/Function_Reference/paginate_links"]]paginate_links[[/LINK]]? Surely that will do exactly what you want...?
Veritus comments:
yes, it didn't work, i don't know why. My php is not the best, i'm not even a webcoder, i'm 3d modeller so nowadays.. i was into web for about 8 years ago but haven't use php so much. I'm giving it a try now for the fun sake but time is little limited.. :)
John Cotton comments:
Where did you put it? It it went after the wp_reset_query then it wouldn't work - it needs to be before that. If the output needs to be after the the wp_reset_query then do
$links = paginate_links(....
wp_reset_query
echo $links;
Then it should work.
Veritus comments:
which code do you mean? then i'll give it a try in the morning..:)
John Cotton comments:
<blockquote>which code do you mean?</blockquote>
You said you'd tried paginate links before. I was asking where in the code above you had put it...