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(); ?>>
Related
I am trying to add a class to <div class="star-rating"> located in content-widget-reviews.php ( in woocommerce template )
displayed here:
<?php echo wc_get_rating_html( intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) ); ?>
I want to make my template work with Bootstrap 4 ( see caps below )
float right needs to be removed vs.
wanted result
I tried this solution below, but it adds the classes on the whole template ( I just want to add this class to the widget area)
// Widget review Stars
function myfuntion_wc_get_rating_html($html) {
// Replace link classes
$html = str_replace('<div class="star-rating"', '<div class="star-rating float-none text-primary"', $html);
return $html;
}
add_filter("woocommerce_product_get_rating_html", "myfuntion_wc_get_rating_html")
;
Is there a way to add my custom class directly on this code ?
<?php echo wc_get_rating_html( intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ) ); ?>
Thanks
P.S. I'd like to avoid the use of Jquery and CSS
Did you try replacing WooCommerce template in your theme like described here https://docs.woocommerce.com/document/template-structure/ ?
Edit files in an upgrade-safe way using overrides. Copy the template
into a directory within your theme named /woocommerce keeping the same
file structure but removing the /templates/ subdirectory.
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>';
}
I'd like to add a title element to a specific page (not all pages). I am using Yoast but I could not find this page on Yoast plugin. So I think I'd have to add it on PHP file head section. But my head section is like this below, how can I add it?
<?php
/**
* The template for displaying all single jobs.
*
* #link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* #package WorkSt
*/
get_header(); ?>
Then below this is HTML, e.g. container and so on.
You can to this by wp_head hook of wordpress
add_action('wp_head', 'add_custom_meta');
function add_custom_meta(){
global $post;
if( 'your-page-slug' === $post->post_name ){
echo '<meta content="your content">'; // add mete here
}
}
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 } ?>
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).