Allowed template for children - wordpress - wordpress

Is there any way to add/change allowed template of page children ? When I create child for specific page I would like to set template to use. I have that option when I use Processwire CMS:
Select the template(s) that will be allowed for children of pages using this template. Use this only if you specifically want to restrict placement of pages using this template. If none are selected then any are allowed, within the user's access limits. An example usage could be a 'news-list' template that is only allowed to have children using 'news-item' or 'press-release' templates.

I have come across this issue before and I have used a custom function to determine what level a page is at and then include a file based on that using:
get_post_ancestors();
The function (added to functions.php)
function hierarchy_level($post){
if(count(get_post_ancestors($post)) === 0){
return 'parent';
}elseif(count(get_post_ancestors($post)) === 1){
return 'child';
}
elseif(count(get_post_ancestors($post)) === 2){
return 'grandchild';
}else{
return false;
}
}
And usage in a page template:
$page_level = hierarchy_level($post);
switch ($page_level){
case 'parent':
include('template-parts/parent.php');
break;
case 'child':
include('template-parts/child.php');
break;
case 'grandchild':
include('template-parts/grandchild.php');
break;
}

Related

View Template Override that is full page

I need to create a template for a view i've created (which I know how to do) but I don't want the page to be wrapped in the drupal html.php code. I want the resulting page to be only what is in the template file I create.
How do I do this?
I did found this which does what I need for content types lab
function yourthemename_preprocess_html(&$vars) {
if ($node = menu_get_object()) {
if($node->type == "lab") {
$vars['theme_hook_suggestions'][] = 'html__lab';
}
}
}
Then created a file called: html--lab.tpl.php
Can this method be used for my view?
does it have to be if($node->type == "lab")?
can I use something like if($view == "viewname")
Thanks
C
I think when you says "created a view" you mean a view + a page display.
So, when you browse to http://mystite.com/mypage, it will show the content of view.
In this case, you can use your code with some small modifications.
function yourthemename_preprocess_html(&$vars) {
if (arg(0) == 'mypage') {
$vars['theme_hook_suggestions'][] = 'html__mypage';
}
}
You will have to create the right .tpl.php file and clear the cache.

Determine if the current Drupal page is a search results page?

How can I determine if the current Drupal page is a search results page?
Assuming clean URLs are on, this usually works
function MYTHEME_preprocess_page (&$vars, $hook) {
// ...
if ($hook == 'page' && arg(0) == 'search') {
// do search related stuff
}
}
inside your template.php.
Normal template suggestions work, too, such as using a page-search.tpl.php instead of page.tpl.php if that is also needed.

wordpress subpage tag

I'm building a wordpress theme right now and I'm using conditionals in the sidebar to display different information for each page like so:
if (is_page('services')) {
etc.....
} elseif (etc....
However, some of the subpages do not have their own specific conditional. How can I make it so that a conditional for a parent page applies to its subpages as well?
Thanks!
You could do this a number of ways. There might be an easier way, but this is what I would do:
On the page itself, check if it is a child page:
if ( is_page() && $post->post_parent )
{
// This is a subpage
return true;
}
else
{
// This is not a subpage
return false;
}
which will check if the page is a child, in which case you can fetch the conditional from the parent the same way you are currently, except specify the parent instead of the current page when checking.
You can also put this snippet of code in your functions.php file and use it anywhere on your wp code.
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
$parentID = $post->post_parent; // the ID of the parent is this
return $parentID; // return the ID
} else { // there is no parent so...
return false; // ...the answer to the question is false
};
};
Source: http://codex.wordpress.org/Conditional_Tags

Drupal: assign block to a specific content type

I made a customized template called node-mynode.tpl.php
Whenever a node of type mynode is requested, then node-mynode.tpl.php
is automatically used.
However, now user wants to see a specific menu block in this case.
Question:
How can I assign a block to a specific content type?
Hint: I have started to look at URL aliases with Pathauto.
I suspect one solution may lie in this direction.
In Drupal 6, you can configure the visibility settings of blocks. Click on the 'configure' link next to your block in the administrator backend and follow these steps -
1) Select the 'Show if the following PHP code returns TRUE (PHP-mode, experts only)' option under the 'Page specific visibility settings' tab.
2) Enter the following PHP code which checks the node type of the current node and returns TRUE accordingly -
<?php
if( arg(0) != 'node' || !is_numeric(arg(1)) )
{ return FALSE;
}
//load a fully-populated Drupal node object
$temp_node = node_load(arg(1));
if( $temp_node->type == 'mynode' ) //check the node type
{ return TRUE; //display block
}
?>
This should work....
you can use the context module
Give all of your mynode type nodes an automatic alias that starts with /mynode and use the page specific visibility settings for the block, showing only on the pages that start with /mynode/*.

drupal views block arguments

I currently have a view (Drupal 6 using Views2) that properly aggregates a custom content type (videos) and filters them for a page display. When I create a block display, it previews the results in live preview just great, but when i go to the page expecting to see the block it doesn't appear.
I'm fairly certain the argument I'm attempting to pass it fails because when I select "Display all results" for "Action to take if argument does not validate:" the block shows up on the page just fine.
Any advice definitely appreciated.
you can use PHP code in the 'provide default argument' section. assuming you're using a menu callback to apply the argument you can do this:
<?php
if(args(0) == 'your-menu-path' && args(1) != ''){
return args(1)
}
?>
make sure you pass a default argument to 'views-embed-view()' if you're using it to place the view on your page otherwise it won't show up at all.
In Drupal 7, to figure out the node you're on, follow corneliusk's answer about making it your default argument. However, do NOT include the php tags, and it's just "arg" not "args" for d7. Ex:
if(arg(0) == 'node' && arg(1) != ''){
return arg(1);
}
else {
return "";
}

Resources