Wordpress paginate_links - index.shtml and page number - php

I am using wordpress and wants to add pagination to custom posts. But I have to maintain post slug by index.shtml. I want to add pagination by index page with page number.
My current URL:
www.demo.com/demo_post/index.shtml
I want to paginate second page URL:
www.demo.com/demo_post/index-2.shtml
Any help would be greatly appreciated, I've been struggling with this. Thanks in advance!

Actually this index-2.shtml is considered as file name so you must have to separate this '2' or '3' or whatever index is there, from the file name. So, you can use string replace method to separate 2 from index-2.shtml, example:
$filename = __FILE__; //return base file name e.g. index-2.shtml
$filename = str_replace("index-","",$filename);
$filename = str_replace(".shtml","",$filename); //this will finally return 2
Now, use this '2'(index) in your pagination function argument. Hope this might help you. :-)

Related

get and add php variable in wordpress page

hi i create one custom page in WordPress like page-download.php
i access this page like
http://example.com/download
its word fine but i want access this page like
http://example.com/download?n=skype
http://example.com/download?n=firefox
etc every time n value change.
and also i want get n value in page-download.php
please tell me how to do it . i know how to work in php simple but in wordpress its not work
Why just not use, echo $_GET['n'] ?
When you hit this page directly from the URL like below, please change your parameter, because of that I have tried same method few days ago and I can't get the parameter value.
http://example.com/download?from_browser=skype
http://example.com/download?from_browser=firefox
You can get the value like below or as per the reference :
$blah = get_query_var( 'from_browser');
if(isset($blah)) // Your code will be here
Hope this help!!!

giving custom argument vlue in 'q' variable

**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.

optional_param in form

I have a question about the optional_param function in a form in PHP (version 5.3.14)
After looking over why certain fields were not being saved in a form I have, I realised that this data...
$checkdata = optional_param('items', array(), PARAM_INT)
Only saves up to 996 places (items) from the form (they are select items and there are many)....
Is this a setting or a something I can change? or alternatively something wrong from my end?
Thanks in advance
Solution : A moodle function (platform i am working with)
Thanks Pekka
this function is a moodle function. It gets a parameter from the current page URL.
For an example url:
http://moodle.dev/course/view.php?id=2&items=4
(this is chosen totally arbitrary)
Using this code:
$checkdata = optional_param('items', array(), PARAM_INT)
Will save the "items" value (here it's 4) in $checkdata. If items does not exist in the url it will do $checkdata = array()

PHP routing with Codeigniter (Titles in URL for SEO)

I have some questions concering routing with Codeigniter. What I´m doing now is the following:
$route['articles/(:num)'] = 'articles/view/$1'; // $1 will contain an ID
This means that example.com/articles/123 will work perfectly and load an article with an ID of 123. But I also want to have the possiblilty to add the aticle´s title to the URL (for SEO). Example: example.com/articles/123/article-title
What I want is pretty much the same thing as Stack Overflow: stackoverflow.com/questions/123/the-title
How can I do that?
I´m also wondering how Stack Overflow works. If I go to stackoverflow/questions/111 the title will automatically be added to the url. Is that done with php redirect()?
I have done something similar in the past; I can't find it know but IIRC (it was months ago) You can use a route like you did, and also add a more specific one, like
$route['articles/(:num)/(:any)'] = 'articles/view/$1/$2';
$route['articles/(:num)'] = 'articles/view/$1';
As you can see, both map to the same method, which is kind of "overloaded"; you can make up for a missing parameter by using a default value:
function articles($id,$slug = FALSE)
{ }
and simply ignore the second parameter in your article retrieval.
As for adding the title you can:
have a "slug" field in your database, created when the article is saved. You can use the comfortable url_title($title,'dash',TRUE) function (in the url helper), which takes the $title, uses the dash as separator, and make it all lowercase;
use the above function and convert the title of the article (after you retrieved it from the database) "on-the-fly"; just check on the article() method if the 2nd parameter isn't false and you'll know if you need to create the slug or not;
As for how to show the slug even when using an url without it you can make, as you guessed, a redirect, but since both routes point to the same method it won't change anything for you.
Oh, uhm, beware of loops while calling the redirect, check carefully ;)
I suggest you to create a slug field into your table and complete it with the url you want to use as id.
Let me explain.
You have this table
id
title
slug
when you save an article into your db you can dinamically create a slug, for example:
id: 1
title: My first post
slug: 1-my-first-post
then you can use the slug (in this case 1-my-first-post) ad id for the page, you can call it:
www.example.com/articles/1-my-first-post
obviusly you need to handle it in your db slect
As we discussed on the comments.
You can create a route several times and with different parameters each, like:
$route['articles/(:num)/(:any)']
$route['articles/(:num)']
I would create a function with a redirect, adding or not the title to it.
Hope it helps.

urls with item title

I was used to putting id's in the URL to map to an item in the database:
/hotels/1
but what if I want to get the name of the hotel with an id of 1 and put it in the URL and replace spaces with hyphens?
/hotels/hotel-bianca
I am using Kohana and there is the concept of routing (which is pretty much present in all MVC frameworks), but I can't seem to get it working
How do I go about doing this?
Since I know nothing about kohana, I am going to present a possible PHP answer.
Could you pass the id through the URL and request it with PHP, and if you're passing the name of the hotel, have that correspond to the item in the database with the hotel-name as its field?
For this purpose I use special field in db table named url :)
So for example /controller/open/urladress will look for url field with 'urladress' inside to open :D
I don't think you can change uri on fly)
In the controller you could search the database for the name. I am used to Kohana 2.3.4 with ORM so this is how I would do it:
// first you need to replace all hyphens with spaces
$name = str_replace('-', ' ', $parameter);
// search your db for the hotel by name
$hotel = ORM::factory('hotel')->where('name', $name)->find();
// check to make sure it is loaded
if ( ! $hotel->loaded)
{
// Do something i.e. 404 page or let them know it wasn't found
}
This would let you specify by name. You could also add a check to see if the parameter was integer or not and search by id if it was.

Categories