Add custom wordpress templete to grandchild pages - wordpress

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

Related

How to add a class to body element if it's a frontpage in WordPress?

last days I have been writing my own WordPress theme, but I run into another problem. These times I have no clue, how to make it possible.
I would like to add a class to every frontpage on my website. So if a single page becomes a frontpage, it will get another class to body tag like "home".
Almost every premium theme gots this funcion, but I just cant find the solution.
Does anybody have any idea?
Thank you! Stepan
You can add the class in body tag using body_class filter as shown below:
function home_body_class($classes) {
if ( is_front_page() ) {
$classes[] = 'home';
}
return $classes;
}
add_filter( 'body_class', 'home_body_class' );
You can manipulate the condition for the static homepage, blog page and so on.

Changing Wordpress template for specific product category page

I'm trying to change the template of a specific product category page, but I can't for the life of me get it to work. I've tried taxonomy-product_cat-the-category but to no avail. I'm using wpml with the categories in 3 different languages if that makes a difference. I've tried with the slugs of each of the 3 langauges but still doesn't work. Don't understand what could be causing this.
First copy taxonomy-product_cat.php & archive-product.php from WooCommerce Plugin directory - wordpress\wp-content\plugins\woocommerce\templates to your theme - wordpress\wp-content\themes\your-theme\woocommerce.
Then make copy of archive-product.php files in your theme's woocommerce directory like archive-product-2.php, archive-product-3.php etc. Later you are going to modify these files as per your category.
Then open taxonomy-product_cat.php file. The code will look like below one.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
We need to modify the template call wc_get_template() in this code with our conditions.
First get the current category slug & then we can compare the slug. As per the slug, we will call different archive product files.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Get current category slug
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
// Call template conditionally
if($cat_slug == 'accessories') {
wc_get_template( 'archive-product-2.php' );
} else {
wc_get_template( 'archive-product.php' );
}
Updates
In my experience, the best approach is avoid WooCommerce template overriding by copying in to theme files. The WooCommerce have regular updates on the templates and it is better to update templates on each time to avoid future issues. So if possible, maximum use filter hooks other than template overriding.
You can can't change the template of WP for a specific category but
For only overriding design: You can do css by using body.term-yourcategoryname at starting of each class, ID or tag name like this:
body.term-car h1 {
color: red;
font-size: 22px;
}
For enhancing Layout: or add some new feature you can use wooommerce default functions like: $term->name; in archive-product.php in your child theme.
example:
if($term->name == "car") {
// your code will goes here
}

How do I exclude some code on posts and pages in wordpress?

I have a feature box on my header.php file, because of this the feature box gets displayed in every page and post page as well. What code do I need to keep this feature box only in the homepage and no place else?
You're looking for the is_front_page() function:
if ( is_front_page() ) {
// Show the feature box
} else {
// Do something else
}

Remove screen options tab if not admin

As the title suggests, I am looking for a way to remove the screen options tab in the post/page editor screen. I have found the following...
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
...but it removes the tab for all users. I would like to keep it for admins.
Regards,
John
Found the answer after collaboration of all of your efforts. Thank you.
get_currentuserinfo() ;
global $user_level;
function remove_screen_options(){ __return_false;}
if( $user_level <= 8 ) add_filter('screen_options_show_screen', 'remove_screen_options');
If you place the following snippet of code into your functions.php file, the Screen Options tab will disappear across the whole backend for all users except the admin. Having said that, it is a good practice to modify the php file of your child theme.
functions.php code:
function remove_screen_options_tab()
{
return current_user_can('manage_options' );
}
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
You just need to conditionally check if the current user is an admin. If they aren't, then remove the screen options like so:
if ( !is_admin() ) {
function remove_screen_options(){ __return_false;}
add_filter('screen_options_show_screen', 'remove_screen_options');
}
Here are the official Wordpress docs detailing this function: http://codex.wordpress.org/Function_Reference/is_admin
Using capabilities the way Spencer suggests is usually the best method.
I'm thinking most people find roles & capabilities to be overly confusing. Using the actual user role with 'current_user_can' is more often than not your best bet for this or any other similar 'permission' based situation.
More often than not you will eventually end up adding/removing capabilities for a specific role, so if you ever give someone else the 'manage_options' capability, like perhaps the owner of the business, you all of a sudden gave them back the screen options as well (On a side note 'activate_plugins' is usually safe since it is only for someone that has level 10 access). You can check out all permissons on the User Level Capability Table at the bottom of the page to get a better grasp on it all.
Insert this in functions php:
if( !current_user_can('administrator') ) {
// hide screen options for everyone but the admin
add_filter('screen_options_show_screen', 'remove_screen_options_tab');
}
if( current_user_can('administrator') ) {
// code here is shown to the admin
}
Following this format you can do the same thing with other roles. Also you dont have is change administrator to editor, author, contributor and subscriber, or any other roles you create.
Try this
if(!current_user_can('manage_options')) add_filter('screen_options_show_screen', 'remove_screen_options');
Use Adminimize, A WordPress plugin that lets you hide 'unnecessary' items from the WordPress backend.
http://wordpress.org/plugins/adminimize/
You're looking for the function current_user_can:
if( current_user_can( 'manage_options' ) ) {
// executes when user is an Administrator
}
Here's the CSS method for all the designers who rather stay away from php. It hooks into the admin_body_class and adds user-{role} as a body class.
functions.php code:
function hide_using_css_user_role( $classes ) {
global $current_user;
foreach( $current_user->roles as $role )
$classes .= ' user-' . $role;
return trim( $classes );
}
add_filter( 'admin_body_class', 'hide_using_css_user_role' );
Using this you can hide/show anything on the admin side per user role. In this case just use the :not css selector to make sure it's only hidden for non-admins.
function add_my_custom_user_css() {
ob_start(); ?>
<style type="text/css">
:not(.user-administrator) #screen-options-link-wrap,
:not(.user-administrator) #contextual-help-link-wrap  {
display:none !important;
}
</style>
<?php
echo ob_get_clean();
}
add_action ( 'admin_head', 'add_my_custom_user_css', 999);
This is a pretty hacky way to do things but sometimes good for a temporary quick fix when you don't know the correct filter/action to hide or change things in wordpress. Adding the 999 will make sure it gets loaded at the end of the head tag. Note that it's only hiding using css, so don't use this for anything vitally important, because the code is still visible in the source file.
To remove it from the source use jquery instead. Just replace the styles above with the following:
<script type="text/javascript">
jQuery(document).ready(function($)) {
$( ":not(.user-administrator) #screen-options-link-wrap" ).remove();
}
</script>
'admin_body_class' already does us the favor of adding the page to body class, so to target specific pages as well, just check the source code and in the body tag you can see the current page. So for example, the dashboard uses .index-php. Just attach that to .user-administrator or whatever user you're targeting and you can customize the admin for any user just using css and javascript.

Wordpress if Child & Grandchild of Page

I'm serving a different header in Wordpress depending what section you are within the site, "residential" , "business" or other by using the following code:
<?php
/*
Template Name: Package
*/
if (is_page('business') || $post->post_parent == "17")
{
get_header('business');
}
else if (is_page('residential') || $post->post_parent == "19")
{
get_header('residential');
}
else
{
get_header();
}
?>
Although I need to extend it to work with grandchildren as well, so another level down. Does anyone have an idea?
You could check to see if the page has an ancestor with the id you specified as the top level parent, i.e.
if(in_array('19', get_ancestors($post->ID, 'page')){...}
Ive done this before a similar way to how you coded originally however I just changed the class and used css to call the image for that class.

Resources