Wordpress URL routing problem - wordpress

I was wondering which is the best approach to get the catgeory ID when listing the posts within a particular category. Normally, the urls look something like this : www.example.com/?cat=4 and it is pretty easy to get the id. However, I really need the urls to be routed like this www.example.com/categories/hotels . wordpress provides an easy way to do the "pretty" routing, however all of the GET paramater information is lost this way. In this case, the $_GET variable is assigned nothing. I need to be able to say $category = $_GET["cat"] or something like that
What is the easiest approach ?

Can you use the Wordpress get_the_category function to grab the ID (from member variable cat_ID) once you're in the template?
See http://codex.wordpress.org/Function_Reference/get_the_category
e.g.
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
// do something with $id
}

The thing is that the guy might not really want to associate categories with post ids. In this case, there's the global $wp->query_vars array that contains all the data coming from the GET request even when the routing has been "prettified"

What about the category base setting in the backend? "Configuration" > "Permalinks" and there the last paragraph. See here for docs.

Related

Which hook do I use, to redirect user based on post/category?

I'm new to WP development. I need to write a hook to check if the currently logged in user is viewing a post listed within a specific category, and then redirect user if they're lacking certain meta data.
I tried creating this function:
add_action('init','check_user_post_category');
however inside that function I was unable to get the post object (I have tried everything I found on the web!)
global $post; // This object is not valid at this time
global $wp; // $wp->request is empty
$_REQUEST; // This var is giving me an empty array! Is this normal??? :(
Could you kindly suggest, what hook is best to use in this case, and how to get the post object? Many thanks!
Use 'wp' hook instead of 'init'.
add_action('wp','check_user_post_category');
Maybe this would work for you.

How can I exploit the wordpress template hierarchy to render a different post template depending on category name/slug?

Apologies if this appears simple to some, but I have scaled high and low and I'm not finding a solution here to my problem, which is:
I have a website set up with Wordpress in which posts can fall under one of three categories: reviews, views, news - the slugs associated with each of these category names are the same.
Currently, calling up the web page of any individual post classified under any of these categories will see it rendered by the file single.php.
However, I want to make a slight adaption to the rendering of the post when it falls within the 'reviews' category. I have copied and renamed the original single.php file to single-post-reviews.php (no custom posts here, I will just confirm and I would like, if possible, to avoid child-theming here - not good practice, I know), but I am not seeing the new rendering from my new file.
I've also tried renaming to single-reviews.php which hasn't worked either - so could someone tell me what exactly I'm missing here?
Thanks,
WordPress Template Hierarchy for Single Posts doesn't factor in the current post category (likely due to the fact you can have multiple categories). Due to this, you have 2 viable options to your problem.
1) You can modify single.php to check for the post category, and if it's categorized under reviews, do something. This makes sense if you're just adding a small amount of markup in one or two places, or even hiding a few lines conditionally.
2) You can override the page template that's loaded based on the post's category using the single_template filter. Because I don't know exactly what you're doing, I'm going to elaborate more on this method. Take the following function:
add_filter( 'single_template', 'so51913799_review_template' );
function so51913799_review_template( $single_template ){
global $post;
if( has_category( 'reviews' ) ){
$single_template = get_stylesheet_directory() . '/single-post-reviews.php';
}
return $single_template;
}
If you put it in your functions.php file, it will make use of the has_category() function (I prefer this to in_category() since in_category just returns has_category anyways) and if it matches, it will update the $single_template variable to single-post-reviews.php. This assumes that the file is inside your /wp-content/themes/ACTIVE-THEME/ directory.

How to check that the a page uses a taxonomy in WordPress?

I need to edit a template's header.php file and I want to check if it has a taxonomy named "store". If so, then I need to extract it's tag_ID.
When I edit the page (it's not a WordPress Page type) I can see these values in the admin url:
taxonomy=store&tag_ID=720
So I know there is a way but I'm having trouble getting any good results. I tried the
method detailed here to extract at least the tag_ID but I'm getting NULL:
$tag_id=get_query_var('tag_ID');
echo $tag_id; //NULL
Edit:
To be clear in regards to the tag_ID because it may be confusing, all I really want is to get the unique id of the requested page so I figure seeing first if it has a taxonomy named "store" and then getting the right one using the tag_id.
AS far as i understand your question, You should try this:-
$data = get_queried_object();
With this, you can get what page/post/taxonomy is being called.
Just print-out/var_dump this $data variable, you will get full object of page/post/taxonomy.
Hope this may help you.

Wordpress permalink without domain name

OK it might sound stange but I am facing a good challenge with wordpress.
when echoing the_permalink(); and checking the portfolio pages, I am getting a link something like this:
http://www.domain.com/?portfolio=test
(Where test is the portfolio name).
Is there any option to return the permalink trimmed to ?portfolio=test keeping out the domain url?
While asking, I think I got the answer already (trim()) but I would like to hear your ideas too.
every answer will be much appreciated!
You can obtain the permalink of a post by doing something like so:
<?php
function my_permalink() {
echo substr(get_permalink(), strlen(get_option('home')));
}
?>
The get_option method replaces the deprecated get_settings method, and allows you to retrieve named option values from the database:
http://codex.wordpress.org/Function_Reference/get_option
The 'home' value passed in to the get_option method will return the site URL.

wordpress dynamic permalink point to a page that takes URL get parameters

I want a structure such as www.training.com/courses/course/location where course and location change to a course name and a geographical location to dynamically translate to www.training.com/course/?course=course&location=location
The reason is I have a plugin to display course information based on course name and location that takes get parameters.
P.s. I don't want to manually add links into a .htaccess file to achieve this.
Are the courses and locations associated with each other? If so...
I'd recommend a custom post type for "courses," with a custom, non-hierarchical taxonomy attached, and then use a custom field in that post type to attach the location information to each course.
The Types plugin would handle all of these things relatively easily (but you'd need to create a template to display that information, of course).
If the URL structure is going to be predictable, you could try:
// $url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$url = "www.training.com/course/?course=course&location=location";
$array = explode('/',$url);
$rev_array = array_reverse($array);
$location = $rev_array[0];
$course = $rev_array[1];
Then edit the plugin code slightly to read these variables rather than get variables.

Resources