Wordpress: Pagination on static Posts page - pagination

I'm having an issue with pagination on a static posts page. Under Reading > Settings, "Posts page" is set to a static page. "Blog pages show at most" is set to 10 posts. The first ten posts show up fine, but then the pagination doesn't work (returns a 404 for "Previous posts"-link).
I've searched for an answer to this question, it seems to be fairly common and it's even mentioned in the WP Codex (http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters), but all the solutions involving modyfying the default query haven't been working for me, e.g:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array('post_type'=>'post','posts_per_page'=>10,'paged'=>$paged);
query_posts($args);
OR
function qm_blog_pagination( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$query->set( 'paged', $paged );
}
}
add_action( 'pre_get_posts', 'qm_blog_pagination' );
Any help would be greatly appreciated.
Thank you,
Pim

Related

How to render and show some of the blog post in one page and rest of them to another page in mongodb

I made blog site using node js and mongodb . When i render blogs to other page all blogs show in one page. But i don't want to show all post in one page , I want to show 10 post in one page and other to the next page. I figured i can limit post using limit() method. But i can only limit and render them to only one page. But how can i render those to next page.
Example i have 20 post, I can render 10 post using limit (10) method to one page. But how can i render and show rest of 10 post to the next page. Please help me if anyone figure it out.
You just need to call skip on the cursor
see MongoDB documentation on cursor skip "This approach may be useful in implementing “paged” results".
This example is taken from Mongodb's example
function printStudents(pageNumber, nPerPage) {
print( "Page: " + pageNumber );
db.students.find()
.skip( pageNumber > 0 ? ( ( pageNumber - 1 ) * nPerPage ) : 0 )
.limit( nPerPage )
.forEach( student => {
print( student.name );
} );
}

Order posts by date or keyword

I want to enable website visitors to order WordPress posts on the category page by date posted or by search keyword, similarly to how it's done on this page:
http://www.steinwaymusical.com/news.php
I'd appreciate a plugin recommendation or any other suggestion from knowledgeable people.
Thank you in advance!
WordPress has order and orderby options you can use in the query.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
<?php
$args = array('order' => 'ASC', 'orderby' => 'name');
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>
Your example page uses GET variables.
?selSort=name_asc&txtKeyword=sdfsdf
So you need to create a form with method="GET" that submit GET data to the current page. Then, using PHP, you can check if any GET data is set (in this case, selSort and txtKeyword). If either of those are set, put them into your query. Then you can modify the query to resemble this:
<?php
$args = array('order' => $_GET['selSort'], 'orderby' => $_GET['txtKeyword']);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
// echo out the title, excerpt
endwhile;
?>

Wordpress Plugin: Facebook comments box position

I have a problem with the "new" Facebook plugin for Wordpress. I would like to re-position it on my post page. I've read somewhere else (or maybe here) that you could use the comment box from developers.facebook.com but I want to use the original Facebook plugin. So here goes:
I've found (in the plugin folder) where Facebook have put the code to install comments box in my theme. It looks like this:
public static function the_content_comments_box( $content ) {
global $post;
if ( ! isset( $post ) )
return;
$options = get_option( 'facebook_comments' );
if ( ! is_array( $options ) || empty( $options ) )
return $content;
// closed posts can have comments from their previous open state
// display noscript version of these comments
$content .= "\n" . self::comments_markup( 'noscript' ) . "\n";
// no option via JS SDK to display comments yet not accept new comments
// only display JS SDK version of comments box display if we would like more comments
if ( comments_open( $post->ID ) ) {
$url = apply_filters( 'facebook_rel_canonical', get_permalink() );
if ( $url ) // false could happen. let JS SDK handle compatibility mode
$options['href'] = $url;
$content .= self::js_sdk_markup( $options );
}
return $content;
}
}
How do put the Facebook comments box in the bottom of my post page?
You can look at a random post page here: My temporary site What I'm ultimately trying to do is to have Facebook Comments box after Relaterede indlæg (related posts).
Is this at all possible?
I don't use the FB plugins for Wordpress because they seem bloated to me. However, here's a simple one-line code that I use to add a facebook comments box. You can place it anywhere in any template.
<div class="fb-comments" data-href="<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ?>"></div>
PLEASE NOTE: this method requires that you have the Facebook SDK installed on the site. Also, it won't work as a widget unless you have enabled the ability to execute PHP in your widgets (requires a plug-in).

Paginator (Migration from Cake 1.3 to 2.0)

I am struggling with the paginator in Cakephp 2.0. While I am trying to migrate my application to 2.0 I cant find any solution to jump directly to the last page. In 1.3 it was quiet to do that from outside like this:
echo $this->Html->link(__('Flights'), array('controller' => 'flights',
'action' => 'index','page' => 'last'));
but this little trick putting 'page:last' in does not work anymore in 2.0. Of course there is a Paginator function called last, but this would only help if I would be already inside the app. My Problem is to access from an outside link directly the last page of the paginator.
This is the simple way:
echo $this->Paginator->last('Any text');
Other way to get the number of the last page is:
echo $this->Paginator->counter(array('format' => '{:pages}'));
Then you can use it to generate your link.
For more info:
http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
Shortly after creating a bounty for this question I found the solution to MY problem using CakePHP 2.2.4. I was trying to accomplish the same task but instead using version 2.2.4 instead instead of 2.0. Basically if I had a link that looked like http://www.domain.com/articles/page:last that the controller's pagination method would know what page to go to and display the correct results (articles) for that page. For example, if I have 110 articles and the pagination limit is set to 25, by going to that URL it would display page 5 of 5, showing records 101-110. I also wanted the same capability if I go to “page:first”.
I needed to change my library file lib/Cake/Controller/Component/PaginatorComponent.php.
I changed
if (intval($page) < 1) {
$page = 1;
}
To
if ((intval($page) < 1 && $page != "last") || $page == "first") {
$page = 1;
}
I also added
if($page == "last"){
$page = $pageCount;
}
After the line
$pageCount = intval(ceil($count / $limit));
Christian Waschke, with this solution, you can use the same link helper exactly how you wrote it in your question. For me, the link helper looked like this
<?php echo $this->Html->link('Go to Last Page', array('controller' => 'articles', 'action' => 'index', 'page' => 'last')); ?>
You can 'calculate' the last page yourself if 'last' is passed as the page number;
I would discourage making modifications in the CakePHP library files as this will make it hard to perform upgrades in the future.
Basically, the PaginatorHelper uses viewVars that are calculated and set by the PaginatorComponent, as seen here: https://github.com/cakephp/cakephp/blob/master/lib/Cake/Controller/Component/PaginatorComponent.php#L212
You can replicate this in your action; for example:
public function index()
{
if (!empty($this->request->params['named']['page'])) {
switch($this->request->params['named']['page']) {
case 'first':
// replace the 'last' with actual number of the first page
$this->request->params['named']['page'] = 1;
break;
case 'last':
// calculate the last page
$limit = 10; // your limit here
$count = $this->Flight->find('count');
$pageCount = intval(ceil($count / $limit));
// replace the 'last' with actual number of the last page
$this->request->params['named']['page'] = $pageCount;
break;
}
}
// then, paginate as usual
$this->set('data', $this->paginate('Flight'));
}
To improve this, this logic should be moved to a separate method, or to a behavior. However; as seen above, it is not required to make modifications in the PaginatorComponent!
Also note that the 'find(count)' in my example does not take additional conditions, they should be added if required
If you have a look in the CakePHP 1.3 source for paginate(), the code above is comparable; https://github.com/cakephp/cakephp/blob/1.3/cake/libs/controller/controller.php#L1204

Drupal autocomplete fails to pull out data as a subdomain

I managed to pull out data using autocomplete at my local (http://mysite.dev/swan/autocomplete). The json data is displayed.
But when I applied the same module at live (now a subdomain: http://test.mysite.com/swan/autocomplete with different drupal installs), this autocomplete fails to pull out data. No json data is displayed.
Do you have any idea if this is related to cross domain issue, or any possible cause I might not be aware of?
This is the callback:
/**
* Callback to allow autocomplete of organisation profile text fields.
*/
function swan_autocomplete($string) {
$matches = array();
$result = db_query("SELECT nid, title FROM {node} WHERE status = 1 AND type='organisation' AND title LIKE LOWER ('%s%%')", $string, 0, 40);
while ($obj = db_fetch_object($result)) {
$title = check_plain($obj->title);
//$matches[$obj->nid] = $title;
$matches[$title] = $title;
}
//drupal_json($matches); // fails at safari for first timers
print drupal_to_js($matches);
exit();
}
Any hint would be very much appreciated.
Thanks
It's the conflict with password_policy.module. Other similar modules just do the same blocking. These modules stop any autocomplete query.

Resources