• Skip to main content
  • Skip to primary sidebar

WordPress, Genesis Framework and Storefront customization tutorials

  • Archive
    • Free
    • Premium
  • Blog
  • About
  • Contact
  • Newsletter
  • Membership
  • Login
Home » PHP

PHP

Hamburger menu for WordPress custom theme

View Demo

Filed Under: Free Tagged With: CSS, JavaScript, PHP

Accordion for ACF Repeater Field

PHP

JS

SCSS

Filed Under: Free Tagged With: JavaScript, PHP, SASS

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;
 }

Filed Under: Free Tagged With: PHP

Display Categories but exclude specific one

Filed Under: Free Tagged With: PHP

How to Display Categories of Current Post

Filed Under: Free Tagged With: PHP

How to Display Tags of Current Post

Filed Under: Free Tagged With: PHP

Show posts with descending order number

Filed Under: Free Tagged With: PHP

How to Show Total Number of Posts, Pages, or Custom Post Types

Filed Under: Free Tagged With: CPT, PHP

Display title of current post’s category

$category = get_the_category(); echo $category[0]-> cat_name; 

Filed Under: Free Tagged With: PHP

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' );

Filed Under: Free Tagged With: PHP

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 ?>

Filed Under: Free Tagged With: ACF, PHP

Enqueue script with WordPress hook

Filed Under: Free Tagged With: PHP

Display post and custom post types categories in one loop

To view the full content, please sign up for the membership.

If You are already a member, please log in below:

 
 
Forgot Password

Filed Under: Premium Tagged With: PHP

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');

Filed Under: Free Tagged With: PHP

Echo something if post has specific category

You should put this snippet inside the loop

if ( in_category('CATEGORY_NAME')){
        echo 'Hello World!';
    }

Filed Under: Free Tagged With: PHP

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.

Add custom text via customizer

Filed Under: Free Tagged With: PHP

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>

Filed Under: Free Tagged With: PHP

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' );

Filed Under: Free Tagged With: PHP

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');

Filed Under: Free Tagged With: PHP

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);

Filed Under: Free Tagged With: Gutenberg, PHP

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »

Primary Sidebar

Gabor Flamich

Hi! I'm Gabor.
I write tutorials on WordPress and WooCommerce.

MacBook

12 Essential Snippets for Genesis Developers

Subscribe to my Newsletter to view my basic collection of Genesis snippets that I use for my projects!

Sign Up for Free
  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
UpdraftPlus Premium

Tags

ACF Ajax Analytics API Bootstrap Breadcrumb category CPT CSS fetch Genesis Google Maps Gutenberg HTML Isotope JavaScript jQuery loop Map Menu Parallax PHP SASS SEO SQL Storefront SVG tab tag manager tags Taxonomy Tool upsell Webpack Wholesale WooCommerce WordPress WPML

Disclosure: Some of the links in this site are affiliate links. I will be paid a commission if you use this link to make a purchase.

  • Privacy Policy / Terms of Service
© 2023 WP Flames - All Right Reserved