I need to add a specific function when child pages of a specific parent page are visualized.
My parent page has ID= 115 and it hase a 7 child pages. I used the following code
global $post; // load details about this page
if(is_page()&&($post->post_parent== '115)) {
echo ' This is a subpage';
}
else {
echo 'This is not a subpage';
}
Though it should work, the output echo, when I visualize one of the child pages, is "This is not a subpage" -> it means that it doesn't recognize it as a child page.
What is wrong? Thank you very much
Want to detect whether a specific Page on a WordPress has children or not?
This function comes in handy, just add it to your functions.php:
function has_subpage() {
global $post;
if($post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
} if ($children) {
echo ' This is a subpage';
} else {
echo ' This is a not subpage';
}
}
You can achieve the desired result by taking the following steps listed below...
Within your child theme, on your page.php first check if your are on a sub page via $post->post_parent.
Then within that, you can return an array of ancestors via get_post_ancestors
Then setup a condition to get the ID of the top level parent page
Once you have the ID, you can easily target the parent page by name. I recommend this so that when your theme travels for your development environment to our live environment, or if you had to manually create pages in any of these environments, the names will be the same.
Finally, enter your code for the sub page that targets its specific parent page location.
if (is_page() && $post->post_parent) :
$parents = get_post_ancestors($post->ID);
$id = ($parents) ? $parents[count($parents) - 1] : $post->ID;
$parent = get_post($id);
$parent_slug = $parent->post_name;
if ($parent_slug == "slug-of-your-specific-parent-page") {
//TEST
echo "this is custom text displaying on my sub page for a specific parent page";
//OR ADD YOUR SPECIFIC FUNCTION
...
}
endif;
I am currently working on a Wordpress theme and need to set custom templates to child and grandchild pages automatically, so a client wouldn't need to worry about selecting which template to choose.
The code I have already is:
<?php
if( $post->post_parent !== 0 ) {
get_template_part('child');
} else {
get_template_part('parent');
}
?>
The code is placed within page.php. This works great for child pages but I need to include something like 'if grandchild apply grandchild.php'.
Any help or advice will be much appreciated. Thanks =D
Try this...
if( count(get_post_ancestors($post->ID)) >= 2 ) {
enter code here
}
Making use of http://codex.wordpress.org/Function_Reference/get_post_ancestors
How can I trace all the relevant tags of a page in the form of classes to the <body> tag? I'm referring to how Wordpress puts the following in a page's body class attribute:
home page page-id-12766 page-template page-template-page-home-php cufon2-disabled safari theme-*
This could be extremely helpful in Joomla template development.
Thanks in advance.
I don't know whether there is any out of the box solutions exists for this. As the main structure of the site is mainly based on the Menu structure, the below snippet may be useful. This is used in the templates/mytemplate/index.php file.
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass= '' ;
//$currentpage = ''; //In case if wanted to get the alias of the current page in a later stage.
if( count($active->tree) > 0 ) {
foreach ($active->tree as $key => $value) {
$pageclass .= 'level-'.$key . ' pageid-'.$value. ' page-'.$menu->getItem($value)->alias ;
//$currentpage = $menu->getItem($value)->alias;
}
}
?>
<body class="<?php echo $pageclass; ?>">
This will output something like:
<body class="level-0 pageid-101 page-home">
You may improve this using various values available in the $active variable.
Here's some information on the Body Class Function
How Wordpress determines which classes to apply using it is found in post-template.php
The relevant function you're looking for is get_body_class within this template. It essentially runs a series of checks to determine if the current page meets the criteria for given classes. It also relies heavily on Wordpress specific function calls that helps make that determination.
I do not want my clients to select a parent page from the drop down list, when creating a child page in Wordpres, so I would like to now if there is a way to create a link on the dashboard which links to "add new page" - but with a preselected parent page?
If this is not possible, then is there a way to change the default parent from "(no parent)" to a parent of my choice?
Put this script in your plugins folder and it'll give new pages a parent of whatever you set as $parent_id
<?php
function set_parent($content) {
/* >> Begin user-configurable variable >> */
$parent_id = '1'; // ID for parent page >> */
/* >> End user-configurable variable >> */
$pattern = "option value='{$parent_id }'";
$replace = "option value='{$parent_id }' selected=\"selected\"";
$content = str_replace($pattern, $replace, $content);
return $content;
}
if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php?post_type=page') ) {
ob_start('set_parent');
}
?>
That should do what you want. And I suppose you could add a dropdown option in somewhere to select parents if a set default parent isn't sufficient like in this example.
This is a tough question to answer sticking with core wordpress ideas. Would you normally have more than one parent page available?
Do you think that about a different way for your client to manage page structure would work?
Admin Column View Plugin is great and allows the user to use drag and drop to reorder pages - http://wordpress.org/plugins/admin-column-view/screenshots/
Hope this helps.
Can I hide the
Edit | View
tabs on top of each node ?
I've searched for this option in theme settings (both global and standard theme but I couldn't find it).
I still want to be able my customer to edit / administer content, so I cannot just remove the permission for it.
thanks
here is a very easy solution for you. (Drupal 7)
Open your page.tpl.php in your current template and search for the $tabs variable.
Remove the render code if you want to hide it completely.
If you want to display it only to administrators use this code
<?php if ($tabs and $is_admin): ?>
<div class="tabs">
<?php print render($tabs); ?>
</div>
The above code checks if the user is administrator. If it is it will render the tabs. If not it wont render them.
This really is a presentational thing, not a functionality thing, so it should be done at the theme level.
The problem with overriding theme_menu_local_tasks() is that you override/take a hatchet to the entire local task display, when you really just want to get in there with a scalpel to remove two specific local tasks. So, you need to get a little more specific.
theme_menu_local_tasks() gets the current page's local tasks and passes them to menu_local_tasks(). Here, two theme functions are used:
theme_menu_item_link(), which gets the link markup for the task
theme_menu_local_task(), which gets the <li> element for the task.
So, you can get rid of the View and Edit local tasks in a really robust way by overriding theme_menu_item_link() and theme_menu_local_task() to include your check for them:
function mytheme_menu_item_link($link) {
// Local tasks for view and edit nodes shouldn't be displayed.
if ($link['type'] & MENU_LOCAL_TASK && ($link['path'] === 'node/%/edit' || $link['path'] === 'node/%/view')) {
return '';
}
else {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
return l($link['title'], $link['href'], $link['localized_options']);
}
}
function mytheme_menu_local_task($link, $active = FALSE) {
// Don't return a <li> element if $link is empty
if ($link === '') {
return '';
}
else {
return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
}
}
This way, you're relying on the menu router path, not modifying the menu router item, and achieving the result you want with minimal changes to core functionality or theming.
On the module side, you could do something that decouples the Edit's menu entry from the local tasks for the node:
function custom_menu_alter(&$items) {
$items['node/%node/edit']['type'] = MENU_CALLBACK;
}
The edit path is still there, but now it is not associated with the View tab. This includes the edit page itself--no View tab there.
there is a module for that: tab tamer allows to hide or disable tabs and rename them as well.
I use the following in template.php by theme (which is perhaps a little hacky, I feel I should be considering unsetting $tabs instead):
function THEME_NAME_menu_local_tasks() {
return '';
}
Or you could ommit:
if ($tabs) echo $tabs;
from your page.tpl.php...
View and Edit are functional features. They have a reason for being there.
The best way to "remove" them, is to "remove" that functionality alltogether. After all: why remove the interface of a piece of functionality, but not the functionality itself?
Besides, simply not printing the tabs, does not remove the url endpoints. In other words: if you don't print the edit tab, people can still access the edit page.
Again: best is to remove that functionality: The fact that you don't want the edit tab, sounds as if you don't want the edit functionality for certain users.
If so, then just remove that permission for that role. That is all. The tabs will be gone.
If, however, you simply wish to display these tabs differently, Drupal is your friends. As you may have noticed, they are called local tasks and not tabs. That is because the theme decides how to render them: The theme is the thing that decides to show them as tabs.
Simply override the theme_menu_local_tasks() to create your own HTML for the "local-tasks". And in your page-tpl, simply move the $tabs variable around to a place, where you want them.
But again: Don't try to change the behavior of the app, by removing interface-elements. That is not the right thing to do: you should change the behavior, in order to change the behavior :)
For all the people stumbling upon this question while looking for a D7 solution: As stated on https://drupal.stackexchange.com/a/77964/15055 it's hook_menu_local_tasks_alter()
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == 'node/%/view') {
unset($data['tabs'][0]['output'][$key]);
}
}
}
This is not the answer to the question of what the author asked. But somehow it might be useful for others user who facing the similar problem with me. Please let me know if this is not suitable to put in here.
I get the answer from #grayside and modified a bit to hide the view | edit tab from node based on the content type I want.
function MYMODULE_menu_alter(&$items) {
$items['node/%node/view']['access callback'] = 'MYMODULE_disable_node_view';
$items['node/%node/view']['access arguments'] = array(1);
}
function MYMODULE_disable_node_view($node){
if($node->type == 'product'){
return false;
}
}
product is the machine name of my content type, I don't want anywant to access it including root user.
The simplest solution to hide the tabs is to add this class in your theme css
.tabs{ display:none;}
Thanks for the last answer. But be aware of this detail: if you try it as-is it cannot work: literals should be just rounded with " or ', not both of them altogether. So it should be:
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == "node/%/view") {
unset($data['tabs'][0]['output'][$key]);
}
}
}
Once taken care of it, it works :)
D8 solution: If you want to hide all "local" tabs on certain pages, remember that "Tabs" is listed in the block library: find it in the "Content" region and exclude by content type, page URL or user role.