I'm trying to add pagination to my wordpress search results.
I was able to locate some information on how to do this - http://codex.wordpress.org/Function_Reference/paginate_links
But I've run into a problem that I can't explain.
The Pagination is being added to the bottom of the search results page and the number of pages does change depending on what search term I use. If you click the next button or select a page number the url changes but the search results don't...
I'm only able to have the first 10 result appear on the page, even if I click page 4, the page will refresh but it still displaying the original 10 results.
Does anyone know how I could fix this?
Here's my code
<?php
get_header();
iinclude_page(608); ?>
<li id="search">
<form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
<div style="text-align:center; margin:20px 0 25px 0;">
<label style="font-weight:bold;" for="s"><?php _e('Search Database:'); ?></label> <input type="text" name="s" id="s" size="20" /> <?php $args = array('hide_empty'=>0,'depth'=>2,'hierarchical'=> 1, );
wp_dropdown_categories($args); ?> <input type="submit" value="<?php _e('Search'); ?>" />
</div>
</form>
</li>
<hr />
<?php
if (have_posts()): ?>
<?php
while (have_posts()) : the_post(); ?>
<li class="postWrapper" id="post-<?php the_ID(); ?>">
<h3 class="entry-title"><?php the_title(); ?></h3>
<div id="postdata">
<div class="search-content">
<?php the_excerpt(__('<p>Read More ></p>')); ?>
</div>
<div class="entry-meta">
<?php
$view_in_browser = '<a class="google-viewer" href="http://docs.google.com/viewer?url='.$attachment_url.'">View document in browser</a>';
$download = '<a class="download" href="'.$attachment_url.'">Download PDF</a>';
echo $view_in_browser . ' ' . $download;
?>
</div>
</div>
<hr />
</li>
<?php endwhile; ?>
<?php
global $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('page','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'plain',
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');
if( !empty($wp_query->query_vars['s']) )
$pagination['add_args'] = array('s'=>get_query_var('s'));
echo paginate_links($pagination);
?>
<?php else: ?>
<h3 style="text-align:center; font-weight:bold; color:#333;"><?php _e('Sorry, no posts matched your criteria.'); ?></h3>
<p style="text-align:center">Please try Searching anohter term.</p>
<?php
endif;
?>
<?php if (will_paginate()): ?>
<?php endif; ?>
<?php
get_footer();
?>
It probably depends on your query and it probably revolves around not using the $paged correctly (cant see your code)
Probably you have one of the 2 improvement to be made in your code:
See: https://wordpress.stackexchange.com/questions/2638/pagination-resolving-to-first-page-only
OR: https://wordpress.stackexchange.com/questions/4368/authors-list-pagination-result-set-from-wpdb-get-results/4370#4370
(and more hits on http://wordpress.stackexchange.com ................)
I was on the search for exactly the same, but couldn't find anything that offered a simple template tag with a semantically correct output (ul) that works well for seo, so i wrote my own one: Easy Pagination Deamon
Related
I'm building a page that is displaying a list of posts (potentially they will be randomly displayed) in a grid.
These posts are a custom post type, and each post is assigned to a single taxonomy for organization.
For each post I'd like to display an associated number. I don't mean the post ID, as that isn't specific enough. Basically, I'd like to treat the custom post type as its own list of posts. So the first post in that post type will be post #1, the second would be #2, and so on.
As well, if it's possible to do this by taxonomy, that would be even better. But I'd settle for just numbering the posts in the custom post type in general.
My solution so far is this: create a function in functions.php that loops through the custom post type, and assigns a number (starting at 1 for the first post) to each post as a custom field. This function runs whenever the page that displays the posts is loaded. So that function runs first in its own loop, and then the page does a normal loop and gets each number.
This solution works cosmetically. I'm getting the result that I want. However, it's not very efficient as it will run any time the page is loaded.
Is there a better way to do this? Perhaps a way to automatically assign a number to a post whenever a post is published? I understand that if a post is deleted, there'd be a skipped number. That's acceptable.
I hope this is clear.
I edited this for clarity, as well as updating the current solution which has changed. I also removed the block of code I have here because it's no longer necessary.
I added 2 lines below, one setting the post count, and one printing the number sequentially look for: $postNumber
<ul id="uselist">
<?php
query_posts( array(
'post_type' => 'use',
'order' => 'desc',
'posts_per_page' => 6
) );
if ( have_posts() ) :
$postNumber = 1; //add a sequential post number!
while ( have_posts() ) : the_post();
$usecategory = get_term_by('id', get_field('use_category'), 'usecategory');
?>
<li>
<div class="use" style="border-bottom:15px solid <?php the_field('category_colour', $usecategory); ?>;">
<div class="useimage" style="background-image:url(<?php the_field('image'); ?>);">
<?php if (get_field('display_vimeo_video') == 'yes') { ?>
<div class="playicon"><img src="<?php echo bloginfo('template_url'); ?>/images/playicon.png" /></div>
<?php } ?>
</div>
<div class="useinfo">
<div class="usecatimage">
<img src="<?php the_field('category_image', $usecategory); ?>" />
</div>
<div class="usecatandtitle">
<h2 class="usecat"><?php echo $usecategory->name; ?> / # <?php echo $postNumber++; ?> //POST NUMBER WILL INCREASE SEQUENTIALLY HERE</h2>
<h3 class="usetitle"><?php the_title(); ?></h3>
</div>
<div class="usename">
<?php the_field('name'); ?>, <?php the_time('M d'); ?>
</div>
</div>
</div>
</li>
<?php
endwhile;
else :
endif;
?>
</ul>
Try this code
EDITED CODE
Add the function in your functions.php file
function Get_Post_Number($postID){
$temp_query = $wp_query;
$postNumberQuery = new WP_Query('orderby=date&posts_per_page=-1');
$counter = 1;
$postCount = 0;
if($postNumberQuery->have_posts()) :
while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post();
if ($postID == get_the_ID()){
$postCount = $counter;
} else {
$counter++;
}
endwhile; endif;
wp_reset_query();
$wp_query = $temp_query;
return $postCount;
}
This is your loop
<ul id="uselist">
<?php
query_posts( array(
'post_type' => 'use',
'order' => 'desc',
'posts_per_page' => 6
) );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$currentID = get_the_ID();
$currentNumber = Get_Post_Number($currentID);
$usecategory = get_term_by('id', get_field('use_category'), 'usecategory');
?>
<li>
<div class="use" style="border-bottom:15px solid <?php the_field('category_colour', $usecategory); ?>;">
<div class="useimage" style="background-image:url(<?php the_field('image'); ?>);">
<?php if (get_field('display_vimeo_video') == 'yes') { ?>
<div class="playicon"><img src="<?php echo bloginfo('template_url'); ?>/images/playicon.png" /></div>
<?php } ?>
</div>
<div class="useinfo">
<div class="usecatimage">
<img src="<?php the_field('category_image', $usecategory); ?>" />
</div>
<div class="usecatandtitle">
<h2 class="usecat"><?php echo $usecategory->name; ?> / Use <?php echo $currentNumber ; ?></h2>
<h3 class="usetitle"><?php the_title(); ?></h3>
</div>
<div class="usename">
<?php the_field('name'); ?>, <?php the_time('M d'); ?>
</div>
</div>
</div>
</li>
<?php
endwhile;
else :
endif;
?>
</ul>
I have not tried this code, but it should work.
I guess you could pull this off easily using Custom Fields. For each post in your custom post type, add a custom meta field, say post_number and manually enter the value for that field.
And, display the value in your page using get_post_meta().
<ul id="uselist">
<?php
query_posts( array(
'post_type' => 'use',
'order' => 'desc',
'posts_per_page' => 6
) );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$usecategory = get_term_by('id', get_field('use_category'), 'usecategory');
?>
<li>
<?php echo get_post_meta($post->ID, 'post_number', true); ?>
<div class="use" style="border-bottom:15px solid <?php the_field('category_colour', $usecategory); ?>;">
<div class="useimage" style="background-image:url(<?php the_field('image'); ?>);">
<?php if (get_field('display_vimeo_video') == 'yes') { ?>
<div class="playicon"><img src="<?php echo bloginfo('template_url'); ?>/images/playicon.png" /></div>
<?php } ?>
</div>
<div class="useinfo">
<div class="usecatimage">
<img src="<?php the_field('category_image', $usecategory); ?>" />
</div>
<div class="usecatandtitle">
<h2 class="usecat"><?php echo $usecategory->name; ?> / Use #</h2>
<h3 class="usetitle"><?php the_title(); ?></h3>
</div>
<div class="usename">
<?php the_field('name'); ?>, <?php the_time('M d'); ?>
</div>
</div>
</div>
</li>
<?php
endwhile;
else :
endif;
?>
</ul>
i'm trying to built a custom archive page.
there is a dropdownmenu for the year, whichs displays the post count (correctly). but when i pick one year, one post (the newest) is missing.
this is my dropdown
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr( __( 'Jahr' ) ); ?></option>
<?php wp_get_archives( array( 'type' => 'yearly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
and there is my loop
<?php while(have_posts()) : the_post(); ?>
<div <?php post_class('post-programm'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="entry-content">
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
i also tried a query_posts, but in this case i get all the posts, not only for one particular year.
<?php query_posts('posts_per_page=-1');
if(have_posts()) : while(have_posts()) : the_post(); ?>
thanks in advance for any help!
Is your one missing post a custom post type? wp_get_archives doesn't work with custom posts.
Right, I been playing around with my code for a bot now, and still can't come up with a solution to my problem.
I have custom post types with custom fields and a custom taxonomy ("lieux") with different terms.
I have created a hierarchical page to display the articles by taxonomy term : taxonomy-lieux.php
This is the code :
<div class="et_pb_column et_pb_column_3_4">
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<div id="container">
<div id="content" role="main">
<h1><?php echo $term->name; ?></h1>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();
$i++;?>
<div class="portfolio<?php if ( ($i%3) == 0 ) { echo ' last'; } elseif ( ($i%2) == 0 ) { echo ' two-last'; } elseif ( ($i%4) == 0 ) { echo ' very-large'; } ?>" data-thematique="<?php echo implode(', ', get_field('thematique')); ?>" data-activite="<?php echo implode(', ', get_field('activite')); ?>" data-niveau="<?php echo implode(', ', get_field('niveau')); ?>">
<div class="project-image"><?php the_post_thumbnail(); ?></div>
<div class="project-info">
<span class="portfolio-title"><?php the_title(); ?></span>
<span class="portfolio-act"><span class="light">NIVEAU :</span> <span class="white"><?php the_field('niveau'); ?></span></span>
<span class="portfolio-act"><span class="light">THÉMATIQUE :</span> <span class="white"><?php the_field('thematique'); ?></span></span>
<span class="portfolio-act"><span class="light">ACTIVITÉ :</span> <span class="white"><?php the_field('activite'); ?></span></span>
<span class="portfolio-act"><span class="light">Lieu : </span><span class="white"><?php echo the_field('lieu1'); ?></span></span></div>EN SAVOIR PLUS
</div>
<?php
endwhile; ?>
<?php endif; ?></div>
</div></div>
Problem is : my page only displays 5 custom post types max ! I think something's wrong with my loop, does anybody know what ?
Thanks a lot !
You can either change the number of posts displayed in Settings | Reading, or you can modify the current page query with:
query_posts( 'posts_per_page=-1' );
-1 will show all posts, or you could put another number in there if you want.
I am using comment_form() in Wordpress, and the comment form is showing up, but the actual comments that have been approved are not:
Here is the code I have
<?php
$comments_args = array(
// remove "Text or HTML to be displayed after the set of comment fields"
'comment_notes_after' => 'Note: Comments are moderated so will not publish immediately.',
// change "Leave a Reply" to "Comment"
'title_reply'=>'What did you think of this story? Please share a comment.',
// change the post comment button
'label_submit' => __( 'Submit your comment' ),
);
comment_form($comments_args);
?>
I have unchecked and checked the right options under Settings -> Discussion
Do I need another call apart from comment_form?
Does it make a difference that this is a custom post type?
Thanks for any help.
Try to use the comments.php file from the twentyfourteen theme. If it works, it is likely that nishant is right with comments probably not being activated for your post type.
TwentyFourteen’s comments.php is also a good example on best practise for comments. You might check it against your other code, if there is some.
It doesn't make any difference if it is a custom post type but you should declare comment support for your custom post type in function.php where you have written arguments for custom post type. e.g. 'supports' => array( 'title' ,'author','comments').
And Use like this in single-post-type.php write <?php comments_template( '', true ); ?> to call comments.php.
And in take your comments.php as backup and replace it with the following -
<?php
/**
* The template for displaying Comments
*
* The area of the page that contains both current comments
* and the comment form. The actual display of comments is
* handled by a callback to twentytwelve_comment() which is
* located in the functions.php file.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() )
return;
?>
<div id="comments" class="comments-area">
<?php // You can start editing here -- including this comment! ?>
<?php if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
printf( _n( 'One thought on “%2$s”', '%1$s thoughts on “%2$s”', get_comments_number(), 'twentytwelve' ),
number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
?>
</h2>
<ol class="commentlist">
<?php wp_list_comments( array( 'callback' => 'twentytwelve_comment', 'style' => 'ol' ) ); ?>
</ol><!-- .commentlist -->
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through ?>
<nav id="comment-nav-below" class="navigation" role="navigation">
<h1 class="assistive-text section-heading"><?php _e( 'Comment navigation', 'twentytwelve' ); ?></h1>
<div class="nav-previous"><?php previous_comments_link( __( '← Older Comments', 'twentytwelve' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments →', 'twentytwelve' ) ); ?></div>
</nav>
<?php endif; // check for comment navigation ?>
<?php
/* If there are no comments and comments are closed, let's leave a note.
* But we only want the note on posts and pages that had comments in the first place.
*/
if ( ! comments_open() && get_comments_number() ) : ?>
<p class="nocomments"><?php _e( 'Comments are closed.' , 'twentytwelve' ); ?></p>
<?php endif; ?>
<?php endif; // have_comments() ?>
<?php //comment_form(); ?>
<?php if ('open' == $post->comment_status) : ?>
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>
<div id="cancel-comment-reply">
<p class="small"><?php cancel_comment_reply_link( __("Cancel Reply", "bonestheme") ); ?></p>
</div>
<div class="cancel-comment-reply">
<?php cancel_comment_reply_link(); ?>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be logged in to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if ( $user_ID ) : ?>
<p>Logged in as <?php echo $user_identity; ?>. Log out »</p>
<?php else : ?>
<p class="comment-notes">Your email address will not be published. Required fields are marked <span class="required">*</span></p>
<p class="comment-form-author"><label for="author">Name <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<input type="text" name="author" id="author" value="<?php //echo $comment_author; ?>" size="22" tabindex="1" /></p>
<p class="comment-form-email"><label for="email">Mail <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<input type="text" name="email" id="email" value="<?php //echo $comment_author_email; ?>" size="22" tabindex="2" /></p>
<p class="comment-form-url"><label for="url">Website</label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /></p>
<?php endif; ?>
<!--<p class="form-allowed-tags"><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<p class="comment-form-comment"><p><label for="comment">Comment <?php if ($req) echo '<span class="required">*</span>'; ?></label>
<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
<p class="form-submit"><input name="submit" type="submit" id="submit" tabindex="5" value="Post Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
</div>
<?php endif; // if you delete this the sky will fall on your head ?>
</div><!-- #comments .comments-area -->
Note- Please change class name and id as per your css
I am trying to add a Slider on my home page using a template from themeforest but I don't get any support there.
I am adding the following code to my header:
<?php get_template_part ( 'includes/featured/featured-call'); ?>
and this code calls featured-call.php and from there another files is called, flexislider.php that looks like this:
<section>
<div class="spaced-wrap clearfix">
<div class="flexslider-container clearfix">
<div class="flexslider-loader">
<div class="flexslider">
<ul class="slides">
<?php
$captioncodes="";
$count=0;
query_posts( array( 'post_type' => 'mtheme_featured', 'showposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC') );
?>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php
$image_id = get_post_thumbnail_id(($post->ID), 'full');
$image_url = wp_get_attachment_image_src($image_id,'full');
$image_url = $image_url[0];
$custom = get_post_custom(get_the_ID());
$featured_description="";
$featured_link="";
if ( isset($custom["featured_bigtitle"][0]) ) $featured_bigtitle=$custom["featured_bigtitle"][0];
if ( isset($custom["featured_description"][0]) ) { $featured_description=$custom["featured_description"][0]; }
if ( isset($custom["featured_link"][0]) && $custom["featured_link"][0]<>"" ) {
$featured_link=$custom["featured_link"][0];
} else {
$featured_link = get_post_permalink();
}
//$textblock=$featured_description;
$title=get_the_title();
$text=$featured_description;
$permalink = $featured_link;
$count++;
?>
<li>
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $image_url; ?>" alt="<?php the_title(); ?>" />
</a>
<?php
$titlecode ='<div class="flex-title">' .$title . '</div>';
$captioncodes ='<div class="flex-caption">' . $text . '</div>';
$bigtitle='<div class="flex-bigtitle">'.$featured_bigtitle.'</div>';
echo '<div class="flex-caption-wrap">';
echo $titlecode;
echo $captioncodes;
echo $bigtitle;
echo '</div>';
?>
</li>
<?php
endwhile; endif;
?>
</ul>
</div>
</div>
</div>
</div>
The problem I have is that once this works, it loads the sliders as posts to the home page and instead of the page I had selected (Home). The page loads fine if I delete the "get_template_part" from header.php, otherwise the sliders come as posts and I don't see the page I selected from reading on wordpress.
My website is http://van-london.com/
I made it!
All I needed was this code after the "get_template_part"
<?php wp_reset_query(); ?>
Done! :)