I'm working on a drupal site, and I need to have an information page for taxonomy data.
The taxonomy data has some extra fields that are displayed, including a custom link.
The default taxonomy page does not allow a custom link, and it will show the content assiociated to the term, I don't want that.
I made a module that outputs a block, and I'm basicly using this code
$term = taxonomy_term_load($termId);
This works fine, but I can't get the translated version of the taxonomy! I'm using the i18n module.
How can I get the localized version of the taxonomy?
Thanks in advance,
Jorik
The langcode parameter of taxonomy_term_view() is used to filter by the language of associated nodes. it is (unfortunately?) not related to the language of the term itself.
You can get the i18n-localized term with
$term = taxonomy_term_load($tid);
if (module_exists('i18n_taxonomy')) {
module_load_include('inc', 'i18n', 'i18n_taxonomy.pages');
$term = i18n_taxonomy_localize_terms($term);
}
print render(taxonomy_term_view($term, 'full'), $language->language);
You need to run your term object through taxonomy_term_view(), that will build the view for you with a particular language code. You can get the 'current' language for the page using the global $language object:
global $language;
$term = taxonomy_term_load($termId);
$view = taxonomy_term_view($term, 'full', $language->language);
$html_output = render($view);
Just a tip, as of writing the Views module doesn't fully support localized terms, as the default taxonomy pages do. You could check out http://drupal.org/project/i18nviews
Related
Was just wondering if there is a way to create a custom variable so that I can add my custom variable created in the Meta title of the page.
Yoast SEO has a list of variables predefined here.
It would be great if I could create a variable of my own. Is there any way to get this?
Thanks in advance!
You have two options for this.
Add filter for change exist variable.
Add your new custom variable.
If you want to change exist variable, you can do it like this:
// define the wpseo_replacements callback
function filter_wpseo_replacements( $replacements ) {
if( isset( $replacements['%%page%%'] ) ){
$replacements['%%page%%'] = 'Page x of y';
}
return $replacements;
};
// Add filter
add_filter( 'wpseo_replacements', 'filter_wpseo_replacements', 10, 1 );
And if do you want to add custom variable you can do it like this:
// define the custom replacement callback
function get_myname() {
return 'My name is Moses';
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some help text' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');
I hope I was helpful to you.
There is a way, but as far as I know, you must get a Premium account in Yoast Seo. One of the most important functions of both Yoast SEO and Yoast SEO Premium is the possibility to add title templates and meta description templates to the homepage, all (custom) post types, all (custom) taxonomies and other pages.
Note: Template variables can also be used on individual posts, pages and taxonomies.
Setting up / changing your templates
You can change your title & meta templates by going to the admin of your WordPress installation and clicking SEO → Titles & Metas.
Log in to your WordPress website. When you're logged in, you will be in your 'Dashboard'. On the left-hand side, you will see a menu. In that menu, click on 'SEO'.
The 'SEO' settings will expand providing you additional options. Click on 'Titles & Metas'.
Under each tab, you can use these variables to create templates for various pages within your site.
You can use the variables from the partial list below to create your own templates for the titles and meta-descriptions. The full list of variables is listed on the HELP tab of the plugin. Just go to SEO → Titles & Metas and click the help tab in the top right.
For more about that and see many images of the process, please visit this page: http://kb.yoast.com/article/146-yoast-wordpress-seo-titles-metas-template-variables
I have added a custom taxonomy to Users - user categories.
Using the code below, I am able to output the custom taxonomies in each edit-user profile page:
function show_user_category( $user ) {
//get the terms that the user is assigned to
$assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
$assigned_term_ids = array();
foreach( $assigned_terms as $term ) {
$assigned_term_ids[] = $term->term_id;
}
//get all the terms we have
$user_cats = get_terms( 'user_category', array('hide_empty'=>false) );
echo "<h3>User Category</h3>";
//list the terms as checkbox, make sure the assigned terms are checked
foreach( $user_cats as $cat ) { ?>
<input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]" value="<?php echo $cat->term_id;?>"/>
<?php
echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
echo '<br />';
}
}
The code above simply displays a list of checkboxes, ordered by term_id.
Of course, I want to display them in the same way that custom taxonomy terms would be displayed in a custom post type (a scrollable list of checkboxes, with child terms indented and underneath their parent term). The code above does not display the terms in order of parents/children.
Is there a WP function I can pass my taxonomy & terms to, to create what I described in the paragraph above? Or do I have to do it manually?
Thanks
Short answer:
Yes, there are. They are called post_categories_meta_box() and post_tags_meta_box(), depending on the type of taxonomy you are using.
Long answer:
This is the sort of question that can be answered by inspecting the WP core code.
Here's the walk-through on how I did it to answer this question:
Editing a page in admin results in a url of wp-admin/post.php. So, inspect that file (post.php).
A quick search in that file for "tax" results in nothing, so we can at least look at the case 'edit' section, because we know we're doing an edit. And we can see it includes a file edit-form-advanced.php.
So, inspect that file (edit-form-advanced.php)
A quick search for "tax" gets into an area that looks promising.
Here we see they call get_taxonomy, and then that get_taxonomy object that is returned has a callback called meta_box_cb - so do a cross-file search for that. We find several instances, but the instance in taxonomy.php is the most promising (because of the file name), so lets look there.
It appears (around line 430 of taxonomy.php) that there's a pair of callback functions - post_categories_meta_box and post_tags_meta_box that WP core is using. Let's check them out - do a cross-file search for function post_categories_meta_box, and we find both of these functions exist in the file meta-boxes.php. The comments for the functions are excellent, and tell us what you are asking: "Display post categories form fields".
Armed with this information, we can ask Google, and we find the WP documentation for the functions:
post_categories_meta_box
and
post_tags_meta_box
This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue
I have a number of views on my website that use a taxonomy term as a contextual filter. I am trying to get that taxonomy term into the page title.
i.e.
'Latest TAXONOMY_TERM News | Sitename etc.'
I am using taxonomy_term_load in a THEME_views_post_render function in my template.php to fetch the term and build the page title. I am then passing this variable to the THEME_preprocess_html where I use it to override $variables['head_title'].
i.e.
views_post_render:
$term = taxonomy_term_load($view->result[0]->_field_data['nid']['entity']->field_term['und'][0]['tid']);
$page_title = 'Latest ' . $term->name . ' News | ' . variable_get('site_name', 'Sitename etc');
preprocess_html:
$variables['head_title'] = $page_title ;
If I echo out $variables['head_title'] I can see the page title I have defined in THEME_views_post_render but it is clearly missing the taxonomy term.
i.e.
'Latest News | Sitename etc.'
Can anyone throw any light on what's happening here? I did think about doing all the logic within the THEME_preprocess_html function, but this doesn't seem to have access to the view.
Any help gratefully appreciated.
Rob.
In view title you can use replacement pattern which will present term-name. using this you can set required page title.
You didn't provide how you are computing/filling the $view variable in your code snippet. Make sure first that you are getting that right. I'll also suggest using Devel module's dpm functionality (or its friends) instead of using echo for your debugging.
I'm trying to make a custom Taxonomy Term page in Drupal 7. I've created a page--taxonomy.tpl.php file in my templates folder. The file only prints out a message. I now try to force the template file by adding
function template_preprocess_page($variables) {
if (arg(0) == 'taxonomy') {
$variables['template_file'] = 'page--taxonomy-tpl';
}
}
in my template.php, but it won't work. Can you help me? And if I get the custom page working, how do I fetch the nodes with this term (in page--taxonomy.tpl.php)? Thanks in advance.
Try using this in your template.php:
function template_preprocess_page(&$variables) {
if (arg(0) == 'taxonomy') {
$variables['theme_hook_suggestions'][] = 'page__taxonomy';
}
}
You need to pass $variables by reference, so add a & before it
template_file has changed to theme_hook_suggestions in Drupal 7
You don't need the -tpl in the template suggestion unless you want it to be a part of the filename like "page--taxonomy-tpl.tpl.php" which I don't think is what you want.
For more information, check out template_preprocess_page(), theme_get_suggestions() and Working with template suggestions
Not sure if this would meet your requirements, but one of default D7 views - Taxonomy term - emulates Drupal core's handling of taxonomy/term pages. You could just enable it (it would automatically replace Drupal's core taxonomy URLs), and then do whatever you want with it, keeping original page structure, all blocks etc, using Views' page templates (see "Theming information" in "Advanced") and all other bells and whistles...
Since you are using Drupal 7, you could also create a file name "taxnomy-term.tpl.php" and edit according to your needs.
See taxonomy-term.tpl.php
Full control over the taxonomy term page can be obtained using hook_menu_alter() . See https://drupal.stackexchange.com/questions/48420/theming-and-overriding-taxonomy-term-vocabulary-page/111194#111194