Friday, October 8, 2010

Wordpress - wp_paginate - Error 404 on pagination when changing posts_per_page on query_posts

To fix 404 page errors while using wp_paginate() in wordpress.
while going to sample-page/page/2 will leads to 404 as the 'page' will be taken as a category.

SO we need to use the 'page' as a query string while using query_posts

Steps 1:
--------
Open the php page (sample: test.php) in a text editor and add this code:

<code><?php
global $query_string;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string.'&paged='.$paged.'&posts_per_page=3'); 
// for getting results of 3rd page

?>

Step:2
------

</code>
  1. Login as the admin
  2. Go to the Settings.
  3. Go to the Reading.
  4. Change the Blog pages show at most to any number. Ex, 5.
  5. Save the changes.
  6. View the blog

wordpress404

If everything goes well, your pagination should be working perfectly fine without you changing any code.

Thursday, September 30, 2010

Trim a string up to character limit without word cut in PHP

Lots of times when you're developing a site, you'll want to have a snippet of text in one place. PHP doesn't really have a good way of doing this because different languages have different definitions of "words", and a space-character is not always the delimiter between words, as it is in most Latin-based languages.

But for those of us speaking languages that stick spaces between words, clients will often ask us to "just show the first 10 words" here or there. So here's a handy PHP function to do just that.

This function can be used to trim a long string up to a given character limit/count without the last word not being cut half.



function trim_me($content, $limit){

$content = substr($content, 0,$limit);
 
$wordlimit = 1000;

$content = explode(' ',$content);

for($i=0; $i<$wordlimit; $i++) $summary[$i] = $content[$i];

$summary = implode(' ', $summary).'...';

return $summary;

}

?>

Sample Usage:

trim_me($sample_string,10);

Popular Posts