PHP
Accordion for ACF Repeater Field
PHP
JS
SCSS
WordPress Customizer phone, email and social links
Display contact info
Display social icons
CSS
ul.social { display: flex; margin: 30px auto; justify-content: space-between; width: 130px; }
Display Categories but exclude specific one
How to Display Categories of Current Post
How to Display Tags of Current Post
Show posts with descending order number
How to Show Total Number of Posts, Pages, or Custom Post Types
Display title of current post’s category
$category = get_the_category(); echo $category[0]-> cat_name;
Enqueue CSS for WordPress admin
// ========================================================================= // WPADMIN CSS // ========================================================================= function admin_css() { wp_enqueue_style( 'admin_css', get_stylesheet_directory_uri() . '/assets/css/wpadmin.css' ); } add_action('admin_print_styles', 'admin_css' );
Query ACF Repeater Field with embedded Group
<!--REPEATER--> <?php if( have_rows('repeater') ): ?> <?php while( have_rows('repeater') ): the_row(); ?> <!--GROUP--> <?php if( have_rows('group') ): ?> <?php while( have_rows('group') ): the_row(); // vars $title = get_sub_field('title'); ?> <?php echo $title; ?> <?php endwhile; ?> <?php endif ?> <?php endwhile; ?> <?php endif ?>
Enqueue script with WordPress hook
Display post and custom post types categories in one loop
<?php wp_reset_postdata(); $args = array( 'post_type' => array( 'post', 'unicorn'), 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => array( 'test' ) ), array( 'taxonomy' => 'unicorn_category', 'field' => 'slug', 'terms' => array( 'blur' ) ) ), ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <h2><?php the_title(); ?></h2> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
Add custom og:image with customizer if latest post is set to the front page
// ========================================================================= // ADD OPEN GRAPH IMAGE UPLOAD OPTION TO CUSTOMIZER // ========================================================================= function og_image_customize_register( $wp_customize ) { // Add Settings $wp_customize->add_setting('customizer_setting_og_image', array( 'transport' => 'refresh', )); // Add Section $wp_customize->add_section('og_section', array( 'title' => __('Open Graph image', 'name-theme'), 'priority' => 70, )); // Add Controls $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'customizer_setting_og_image', array( 'label' => __('Add Open Graph image', 'name-theme'), 'section' => 'og_section', 'settings' => 'customizer_setting_og_image', ))); } add_action('customize_register', 'og_image_customize_register'); // ========================================================================= // HOOK THE OG IMAGE INTO THE HOME PAGE // ========================================================================= function add_og_image_to_homepage() { if(is_front_page()){ $og_image = esc_url( get_theme_mod( 'customizer_setting_og_image' ) ); echo '<meta property="og:image" content="'.$og_image.'" />'; } } add_action('wp_head', 'add_og_image_to_homepage');
Echo something if post has specific category
You should put this snippet inside the loop
if ( in_category('CATEGORY_NAME')){ echo 'Hello World!'; }
Create a text field in the Customizer and show it in the front end
1) Add this to your functinos.php
// ========================================================================= // REGISTER CUSTOMIZER - PANEL, SECTION, SETTINGS AND CONTROL // ========================================================================= function theme_name_register_theme_customizer( $wp_customize ) { // Create custom panel. $wp_customize->add_panel( 'text_blocks', array( 'priority' => 10, 'theme_supports' => '', 'title' => __( 'Text Blocks', 'theme_name' ), 'description' => __( 'Set editable text for certain content.', 'theme_name' ), ) ); // Add section. $wp_customize->add_section( 'custom_title_text' , array( 'title' => __('Custom Text','theme-name'), 'panel' => 'text_blocks', 'priority' => 10 ) ); // Add setting $wp_customize->add_setting( 'title_text_block', array( 'default' => __( 'Default text', 'theme-name' ), 'sanitize_callback' => 'sanitize_text' ) ); // Add control $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'custom_title_text', array( 'label' => __( 'Custom Text', 'theme_name' ), 'section' => 'custom_title_text', 'settings' => 'title_text_block', 'type' => 'text' ) ) ); // Sanitize text function sanitize_text( $text ) { return sanitize_text_field( $text ); } } add_action( 'customize_register', 'theme_name_register_theme_customizer' );
2) Show in the Front End with a hook
function add_custom_field(){ echo get_theme_mod( 'title_text_block'); } add_action('genesis_site_description', 'add_custom_field');
In this case I’ve placed the custom text in the site-description, but You can put it anywhere You want using the Genesis Visual Hook Guide.
How to add PDF upload option to WordPress customizer?
1) Add this snippet to functions.php
// ========================================================================= // CUSTOMIZER PDF UPLOAD // ========================================================================= function pdf_customize_register( $wp_customize ) { // Add Settings $wp_customize->add_setting('customizer_setting_pdf', array( 'transport' => 'refresh' )); // Add Section $wp_customize->add_section('pdf_section', array( 'title' => __('PDF', 'name-theme'), 'priority' => 70, )); // Add Controls $wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, 'customizer_setting_pdf', array( 'label' => __('PDF Upload', 'name-theme'), 'section' => 'pdf_section', 'settings' => 'customizer_setting_pdf', ))); } add_action('customize_register', 'pdf_customize_register');
2) Displaying in the Front End
<a href="<?php echo esc_url( get_theme_mod( 'customizer_setting_pdf' ) ); ?>">Download PDF</a>
Add page slug to body_class
// ========================================================================= // 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' );
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');
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);