Conditional statements for custom template - php

I have 2 template files , One template file is front-page.php and the other template is a custom one. Basically the code below is stating if it is front page then use this body class but if its anything else then use other body class. What I am trying to do is have my new template use the same body class as my front-page.php. I am having some issues making this occur. So basically,it needs to be "if front page or custom-template.php then use body class everything else "innerbody"
<?php if(w3c()){ ?><?php if(is_front_page() || is_page_template ('page-templates/front-page.php')){?><body> <?php } else{ ?><body id="innerbody"><?php } ?>

Generally used function for this is body_class(), and is used typically within the header.php template in the HTML body tag. Default classes that this function add are based upon a particular page that is displayed, so that they themselves are the solution for most of the stuff.
You can add additional body classes by using the body_class filter, add the following to the functions.php file:
add_filter( 'body_class', function( $classes ) {
// add custom class to the $classes array and return the $classes array
if ( is_front_page() || is_page( 'page-slug' ) ) {
$classes[] = 'innerbody';
}
return $classes;
});
Now you can use conditional tags inside that function to test the conditions that must be met to in order to take the specific action, in this case to add additional classes to the body tag. I recommend that you check the Template Hierarchy documentation also, it is the first step towards understanding the concept of themes and templates in WordPress.

Simply add another is_page_template() call to your if() function, and another OR (||) in your if().
<?php if(w3c()){ ?>
<?php if(is_front_page() || is_page_template ('custom-template.php') || is_page_template ('page-templates/front-page.php')){?>
<body>
<?php } else{ ?>
<body id="innerbody">
<?php } ?>

Related

if body tag has class 'home' via child theme

I'm trying to simply show content with a php if statement.
I would like to add a footer element (to the footer.php) for the homepage only.
So, if body tag has class 'home'; then add new footer element. I'm trying below; but currently nothing is displaying at all.
$classes = get_body_class();
if (in_array('home',$classes)) {
function lastUpdated() {
echo '<div class="last-updated">Last Updated:<span class="date-update"></span></div>';
}
}
the above code is in my child theme's functions.php file;
First of all, how WP will know, where you want to output that markup?
To solve this question you can use hooks, you need to monitor parent theme for such code do_action('before_theme_footer'), to find place where you can "inject" your code.
And then you can apply your output to this hook, and inside callback function check if you on home page with is_front_page():
function footer_markup(){
if( is_front_page() ) {
echo '<div></div>';
}
}
add_action('before_theme_footer', 'footer_markup');
You can read about hooks here - https://designmodo.com/wordpress-hooks-filters/
If no any hooks in footer.php, you can copy footer.php to child theme and add your code in the right place where you want to output it:
if( is_front_page() ) {
echo '<div></div>';
}

Home Template Loop customization - should fetch content based on post template

In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!

WordPress body_class() not adding CSS classes

Recently I have been working on creating my first wordpress theme. However, when I was trying to add classes to body tag using PHP, the Chrome inspector shows that no classes have been added at all.
I have tried to move the body_class() PHP line out of the tag, and the appropriate class="(some classes)" loaded up successfully, but when the tag include the PHP scripts, no classes were added.
Manually adding classes inside the body tag in header.php does not work as well.
Is there any solution? Because my header.php should be following the right logic.
A link to my files:
https://drive.google.com/open?id=1h9YvmYEQYiHWI-N0lkelAUbVbEQwTelz
try that
/**
* Adds custom classes to the array of body classes.
*
* #param array $classes Classes for the body element.
* #return array
*/
function White_theme_body_classes( $classes ) {
// Adds a class of group-blog to blogs with more than 1 published author.
if ( is_multi_author() ) {
$classes[] = 'group-blog';
}
// Adds a class of hfeed to non-singular pages.
if ( ! is_singular() ) {
$classes[] = 'hfeed';
}
return $classes;
}
add_filter( 'body_class', 'White_theme_body_classes' );
and in header.php add this code
<body <?php body_class(); ?>>

is_home not working in wordpress when using templates

I have a div of services (full width) which I only want to display on the home page of my Wordpress site. I have used the conditional tag if(is_home()) and it is working fine. But when I added a new template this division is showing up in the template page, too. I tried using:
(if(is_home())&&(!is_page_template('blog.php'))
...but unfortunately it is not working. I have also tried it using ID and slug, and yet still this particular div is coming in the template.
I some how want this div to show up only on the homepage. You can see the services div here. The same is being displayed here, but not in other inner pages.
Use is_front_page() instead.
See Here
This code
<?php if (is_home()) : ?>
// yes, home page
<?php else : ?>
//no, not home page
<?php endif; ?>
Simply use
if(is_home() && !is_page_template('blog.php'))
Everything Should be in one condition
You can do it with any of the following conditional tags
if( is_home() && !is_page_template( 'page- template-Blog-php' ) ) {
// service div here
}
Note: If you are using template in any folder you need to give folder path too.
For example: If you are placing all the templates in 'templates' folder your code will be:
if( is_home() && !is_page_template( 'templates/page-template-Blog-php' ) ) {
// service div here
}
or you can try following code:
if( is_home() || is_front_page() ) {
// service div here
}
Braces wrongly placed:
if( (is_home()) && (!is_page_template('blog.php')) )

Add a custom class name to Wordpress body tag?

I'd like to place a directive in my theme's functions.php file which appends a classname to the wordpress body tag. Is there a built-in API method for this?
For example, my body tag code is...
<body <?php if(function_exists("body_class") && !is_404()){body_class();} else echo 'class="page default"'?>>
And it results in the following being written to the body tag (depending on the context in which the page is presented (page, post, logged-in, etc)
<body class="home blog logged-in">
Depending on the child theme I'm using at the time, I want it to be...
<body class="home blog logged-in mychildthemename">
You can use the body_class filter, like so:
function my_plugin_body_class($classes) {
$classes[] = 'foo';
return $classes;
}
add_filter('body_class', 'my_plugin_body_class');
Although, obviously, your theme needs to call the corresponding body_class function.
You can also use it directly inside the WP body_class function which will append the string inside body class.
eg.
$class="custom-class";
<body <?php body_class($class); ?>>
http://codex.wordpress.org/Function_Reference/body_class
To add more classes to the filter, just add another line that adds another value in to the array:
add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
$classes[] = 'class-name';
$classes[] = 'class-name-two';
return $classes;
}
In case you're trying to add classes to the body tag while in the Admin area, remember to use the admin_body_class hook instead. Note that it's a filter which works slightly different since it passes a string of classes rather than an array, so your code would look like this:
add_filter('admin_body_class', 'my_admin_body_class');
function my_admin_body_class($classes) {
return $classes . ' my_class';
}
Try this..
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( !is_front_page() ) {
$classes[] = 'example';
}
return $classes;
}
If you just want to add class based on your page template note that WP now automatically adds per-template class to body tag.
Simply edit your theme's header.php and change the class there (manually or according to some preset logic rules).

Categories