PHP
How to Show Total Number of Posts, Pages, or Custom Post Types
AJAX Filter for WordPress Custom Post Type Taxonomy
AJAX Filter for WordPress Categories without Plugin
AJAX request from MySQL database
AJAX – Submit data with POST request into a MySQL database
Display Google Maps location address details of ACF field without coordinates
<?php $location = get_field('location'); if( $location ) { // Loop over segments and construct HTML. $address = ''; foreach( array('street_number', 'street_name', 'city', 'state', 'post_code', 'country') as $i => $k ) { if( isset( $location[ $k ] ) ) { $address .= sprintf( '<span class="segment-%s">%s</span>, ', $k, $location[ $k ] ); } } // Trim trailing comma. $address = trim( $address, ', ' ); // Display HTML. echo '<p>' . $address . '.</p>'; }
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' );
Get videos from channel by YouTube Data API
Remove Entry Meta from Genesis Single Post
//Remove Entry Meta from Genesis Single Post add_action( 'loop_start', 'remove_entry_meta' ); function remove_entry_meta() { if ( is_singular('post') ) { remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); } }
Custom Post Type Taxonomy Filter in Admin
<?php add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy'); function tsm_filter_post_type_by_taxonomy() { global $typenow; $post_type = 'YOUR-POST-TYPE'; // change to your post type $taxonomy = 'YOUR-TAXONOMY'; // change to your taxonomy if ($typenow == $post_type) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); wp_dropdown_categories(array( 'show_option_all' => __("Összes kategória"), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => true, )); }; } add_filter('parse_query', 'tsm_convert_id_to_term_in_query'); function tsm_convert_id_to_term_in_query($query) { global $pagenow; $post_type = 'YOUR-POST-TYPE'; // change to your post type $taxonomy = 'YOUR-TAXONOMY'; // change to your taxonomy $q_vars = &$query->query_vars; if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) { $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); $q_vars[$taxonomy] = $term->slug; } }
How to set Featured Image as Background Cover with Title and Sub Title
Create a hook in functions.php
//ADD COVER TO ONLY SUB PAGES function add_background_cover(){ if ( !is_front_page() ) { include('inc/cover.php'); } } add_action('genesis_after_header', 'add_background_cover');
Create a file inc/cover.php and place the following snippet in it. Don’t forget to replace the ACF slug in cover_subtitle() function!
<?php //Cover Background function cover_background() { function cover_subtitle() { if( get_field('........') ){ the_field('........') } else{ echo 'Hello World!'; } } $bgImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); if( has_post_thumbnail() ){ ?> <section class="cover" style="background-image: url('<?php echo $bgImage[0]; ?>'); "> <h1 class="entry-title" itemprop="headline"><?php the_title(); ?></h1> <p><?php cover_subtitle(); ?></p> </section> <?php } else{ ?> <section class="cover" style="background-image: url(..............); "> <h1 class="entry-title" itemprop="headline"><?php the_title(); ?></h1> <p><?php cover_subtitle(); ?></p> </section> <?php } } cover_background(); ?>
CSS
.cover { height: 280px; padding-top: 60px; }
Hide something if user logged out / Show only for logged in users
Put these snippet in functions.php
//HIDE IF LOGGED OUT function hide_if_logged_out(){ if ( is_user_logged_in() ) { echo ''; } else { echo 'hidden'; } }
Call the function as class
<div class="container <?php echo hide_if_logged_out(); ?>">
If there’s no post in the query
<?php endwhile; else: echo 'There is no posts yet.'; endif; ?>
Loop Zillow CPT
<?php $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="review-content"> <?php include('stars-main.php'); ?> <label class="highly"> <?php if( get_field('main_review') == '5') : ?> Highly likely to recommend <?php endif; ?> <?php if( get_field('main_review') == '4') : ?> Likely to recommend <?php endif; ?> <?php if( get_field('main_review') == '3') : ?> Might recommend <?php endif; ?> </label> <br> <ul class="review-metadata"> <li><?php the_title(); ?></li> <li><?php the_field('short_description'); ?></li> <li> <?php if( get_field('primary_point_of_contact') ): ?> <li><?php the_field('primary_point_of_contact'); ?></li> <?php endif; ?> </li> </ul> <div class="reviews-sub-ratings"> <ul class="ratings"> <li><span class="ratings-col">Local knowledge:</span> <?php include('stars-local.php'); ?></li> <li><span class="ratings-col">Process expertise:</span> <?php include('stars-process.php'); ?></li> <li><span class="ratings-col">Responsiveness:</span> <?php include('stars-respo.php'); ?></li> <li><span class="ratings-col">Negotiation skills:</span> <?php include('stars-nego.php'); ?></li> </ul> </div> <div class="review-body"> <?php the_field('review'); ?> </div> </div> <?php endwhile; else: echo 'No reviews yet.'; endif; ?> <?php wp_reset_postdata(); ?>
ACF hide if empty custom field
<!--ACF HIDE IF EPMTY CUSTOM FIELD --> <?php if( get_field('mobil') ): ?> <li><?php the_field('mobil'); ?></li> <?php endif; ?>
<!--IF ELSE HIDE - SHOW DIV IF THE_FILED IS EMPTY!!!!!!!--> <?php if( get_field('hogyan_keszult_link') ){ ?> <style type="text/css">.hogy-keszul-cta{ display:block; }</style> <?php }?>
//CUSTOM FIELDS SELECET OPTIONS <?php if( get_field('color') == 'red' ): ?> <p>Selected the Red choice!</p> <?php endif; ?>
Translate without PO Edit
<?php function wpf_filter_gettext( $translated, $original, $domain ) { if ( $translated == "OLD_TEXT" ) { $translated = "NEW_TEXT"; } return $translated; } add_filter( 'gettext', 'wpf_filter_gettext', 10, 3 );
Remove translation
function remove_translation( $translated_text, $untranslated_text, $domain ) { $removable_text = 'Ide kerül az eltávolítani kívánt kifejezés.'; if ($untranslated_text === $removable_text ) { return ''; } return $translated_text; }
PHP date format
Required. Specifies the format of the outputted date string. The following characters can be used:
- d – The day of the month (from 01 to 31)
- D – A textual representation of a day (three letters)
- j – The day of the month without leading zeros (1 to 31)
- l (lowercase ‘L’) – A full textual representation of a day
- N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
- S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
- w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
- z – The day of the year (from 0 through 365)
- W – The ISO-8601 week number of year (weeks starting on Monday)
- F – A full textual representation of a month (January through December)
- m – A numeric representation of a month (from 01 to 12)
- M – A short textual representation of a month (three letters)
- n – A numeric representation of a month, without leading zeros (1 to 12)
- t – The number of days in the given month
- L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
- o – The ISO-8601 year number
- Y – A four digit representation of a year
- y – A two digit representation of a year
- a – Lowercase am or pm
- A – Uppercase AM or PM
- B – Swatch Internet time (000 to 999)
- g – 12-hour format of an hour (1 to 12)
- G – 24-hour format of an hour (0 to 23)
- h – 12-hour format of an hour (01 to 12)
- H – 24-hour format of an hour (00 to 23)
- i – Minutes with leading zeros (00 to 59)
- s – Seconds, with leading zeros (00 to 59)
- u – Microseconds (added in PHP 5.2.2)
- e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
- I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
- O – Difference to Greenwich time (GMT) in hours (Example: +0100)
- P – Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
- T – Timezone abbreviations (Examples: EST, MDT)
- Z – Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
- c – The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
- r – The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
- U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
Add text via function
//ADD TEXT VIA FUNCTION add_filter( 'ultimatum_print_footer', 'add_barion_banner' ); function add_barion_banner( $barion_banner ) { echo 'TESZT'; return $barion_banner; }