// =========================================================================
// ADD PAGE SLUG TO BODY CLASS
// =========================================================================
function add_slug_to_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_to_body_class' );
Free
How to create custom Hooks in WordPress
1) Define the custom hook in functions.php
function latest_products_hook() {
do_action('latest_products_hook');
}
2) Place anywhere you want in your template files
<?php latest_products_hook(); ?>
3) Create the function
add_action( 'latest_products_hook', 'storefront_recent_products', 30);
How to change the color of Ninja Form placeholder
.nf-field-container .field-wrap .nf-element::placeholder {
color: orange;
}
How to remove ‘Menu’ text in Genesis mobile navigation
1) You need to find this file: genesis-sample/config/responsive-menus.php
2) Add this line of code before the line menuClasses:
'mainMenu' => __( '', 'genesis-sample' ),
3) The full code need to look like this:
return [
'script' => [
'mainMenu' => __( '', 'genesis-sample' ),
'menuClasses' => [
'others' => [ '.nav-primary' ],
],
],
'extras' => [
'media_query_width' => '960px',
],
];
How to force hamburger menu on desktop in Genesis
All You need to do is just add some rows of CSS:
.genesis-responsive-menu {
display: none;
}
.menu-toggle, .sub-menu-toggle {
display: block;
visibility: visible;
}
How to get Advanced Custom Field slug
<?php $anchor = strtolower(str_replace(array(' ', ':'), array('-', ''), get_sub_field('title'))); ?>
<a href="#<?php echo $anchor; ?>"><?php the_sub_field('title'); ?></a>
<h2 id="<?php echo $anchor; ?>" class="headline"><?php the_sub_field('title'); ?></h2>
Disallow PNG upload in WordPress
In this snippet the are going to allow to upload only jpg and pdf format.
// =========================================================================
// DISALLOW PNG, GIF, TIF
// =========================================================================
function theme_restrict_mime_types( $mime_types ){
$mime_types = array(
'pdf' => 'application/pdf',
'jpg|jpeg' => 'image/jpeg',
);
return $mime_types;
}
add_filter( 'upload_mimes', 'theme_restrict_mime_types' );
Add Image Size to Advanced Custom Fields
add_image_size('pageBanner', 1500, 300, true);
$pageBannerImage = get_field('page_banner_image');
echo $pageBannerImage['sizes']['pageBanner'];
<?php echo wp_get_attachment_image_src( get_field('field_name'), 'medium' )[0]; ?>
<?php
$attachment_id = get_field('image');
$size = 'gallery-thumb';
$image = wp_get_attachment_image_src( $attachment_id, $size )[0];
?>
<div class="col-md-3">
<div class="card">
<div class="card-img-top">
<a href="<?php the_field('link'); ?>">
<img src="<?php echo $image; ?>" alt="<?php the_field('title'); ?>">
</a>
</div>
<div class="card-body">
<h4 class="card-title"><?php the_field('title'); ?></h4>
</div>
</div>
</div>
How to Make Custom Post Type Child of Page
'rewrite' => array(
'slug' => get_post_field( 'post_name', get_option('cpt_parent_page') ) . '/PARENT/YOUR_POST_TYPE',
'with_front' => false
)
Change the Structure of Custom Post Type’s Breadcrumb in Single Page
// =========================================================================
// FIX BREADCRUMB IN CUSTOM POST TYPE
// =========================================================================
function add_custom_breadcrumb(){
if(is_singular('YOUR_CPT')){
if ( function_exists('yoast_breadcrumb') ) :
$post_types = array('YOUR_CPT');
?>
<div class="breadcrumb custom">
<span xmlns:v="http://rdf.data-vocabulary.org/#">
<span typeof="v:Breadcrumb"><a href="<?php echo home_url(); ?>/" rel="v:url" property="v:title">Home</a></span> /
<a href="#">Page 1</a> /
<a href="#">Page 2</a> /
<a href="#">Page 3</a> /
<?php the_title(); ?>
<span typeof="v:Breadcrumb"><strong class="breadcrumb_last" property="v:title"><?php post_type_archive_title(); ?></strong></span>
</span>
</div>
<?php
else :
yoast_breadcrumb('<div class="breadcrumb"> ','</div>');
endif;
}
}
add_action('genesis_before_loop', 'add_custom_breadcrumb');
Add Three Column Layout to Genesis Front Page and Two Column for Other Pages
1) Go to Customizer / Theme Settings/ Site Layout / Select Site Layout to Secondary Sidebar – Content – Primary Sidebar
2) Add this snippet to functions.php
// =========================================================================
// ADD SECONDARY SIDEBAR - CONTENT - SIDEBAR - 3 COL LAYOUT TO FRONT-PAGE
// =========================================================================
function add_three_column_layout_to_frontpage() {
if ( !is_front_page() ) {
return 'content-sidebar';
}
}
add_filter( 'genesis_pre_get_option_site_layout', 'add_three_column_layout_to_frontpage' );
Disable Gutenberg only for Posts
// =========================================================================
// DISABLE GUTENBERG FOR POSTS
// =========================================================================
function wpflames_disable_gutenberg($is_enabled, $post_type) {
if ($post_type === 'post') return false; // it could be any kind of custom post type
return $is_enabled;
}
add_filter('use_block_editor_for_post_type', 'wpflames_disable_gutenberg', 10, 2);
ACF Post Object with Multiple Options
<?php
/*
* Loop through post objects (assuming this is a multi-select field) ( don't setup postdata )
* Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
*/
$post_objects = get_field('YOUR_CUSTOM_FIELD');
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post_object): ?>
<li>
<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
?>
ACF Post Object for Related Posts with Repeater Field
<?php if( have_rows('ITEMS') ): ?>
<?php while ( have_rows('ITEMS') ) : the_row(); ?>
<?php $post_object = get_sub_field('ITEM'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<h4><?php echo get_the_title($post_object->ID); ?></h4>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Add sub-page to body_class if it’s not front-page
// =========================================================================
// ADD SUBPAGE BODY CLASS
// =========================================================================
function body_class_subpage($classes) {
global $wpdb, $post;
if(!is_front_page()){
$classes[] = 'sub-page';
}
return $classes;
}
add_filter('body_class','body_class_subpage');
Simple Bar – Scroll Bar
funtcions.php
// =========================================================================
// SIMPLE BAR
// =========================================================================
function simple_bar() {
if(is_front_page()){
wp_enqueue_style('simple_bar_css', 'https://unpkg.com/simplebar@latest/dist/simplebar.min.css');
wp_enqueue_script('simple_bar_js', 'https://unpkg.com/simplebar@latest/dist/simplebar.js', array(), '4.1.0', true );
}
}
add_action( 'wp_enqueue_scripts', 'simple_bar' );
HTML
<div class="demo1" data-simplebar>
<h2>Scroll me!</h2>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Scroll me!</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Scroll me!</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
<p class="odd">Some content</p>
<p>Some more content</p>
</div>
CSS
.demo1 {
height: 376px;
max-width: 100%;
}
Remove “You are here” from Genesis breadcrumb
// =========================================================================
// REMOVE YOU ARE HERE FROM BREADCRUMB
// =========================================================================
add_filter( 'genesis_breadcrumb_args', 'afn_breadcrumb_args' );
function afn_breadcrumb_args( $args ) {
$args['labels']['prefix'] = '';
return $args;
}
Remove menu items from WordPress admin
Remove menu items for specific user roles
Genesis translations
<?php
// =========================================================================
// GENESIS TRANSLATIONS WITHOUT LOCO TRANSLATE
// =========================================================================
function genesis_hungarian_translations( $translated, $original, $domain ) {
if ( $translated == "Leave a Comment" ) { $translated = "Hozzászólás írása"; }
if ( $translated == "by" ) { $translated = "Szerző: "; }
if ( $translated == "Filed Under: " ) { $translated = "Kategória: "; }
if ( $translated == "Tagged With: " ) { $translated = "Címkék: "; }
return $translated;
}
add_filter( 'gettext', 'genesis_hungarian_translations', 10, 3 );
Add category and tag support to pages in WordPress
// =========================================================================
// ADD CATEGORY SUPPORT TO PAGES
// =========================================================================
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
// =========================================================================
// ADD TAG SUPPORT TO PAGES
// =========================================================================
function tags_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
}
add_action('init', 'tags_support_all');