Skip to content

Instantly share code, notes, and snippets.

@AntonLitvin
AntonLitvin / WP redirect
Created November 16, 2020 08:03
Переадресация для страниц с разными урлами CPT
add_action( 'template_redirect', 'rider_redirect_canonical' );
function rider_redirect_canonical(){
if ( is_single() && ('post' === get_post_type()) ) {
$tax = 'category';
// проверяем наличие терминов, иначе редирект бессмысленный
if ( $terms = get_the_terms(get_the_ID(), $tax) ) {
@AntonLitvin
AntonLitvin / Widget menu Walker
Created November 13, 2020 10:23
Different options for menu in widgets
function widget_nav_args( $nav_menu_args, $nav_menu, $args, $instance ) {
if ( $args['id'] === 'header-menu' ) { // ID сайдбара, где располагается виджет с меню
return array_merge( $nav_menu_args, array(
// аргументы, передаваемые в меню, например:
'walker' => new My_Custom_Walker(),
));
}
return $nav_menu_args;
}
@AntonLitvin
AntonLitvin / Analitics timeout.js
Created November 10, 2020 10:08
Задержка загрузки скриптов, влияющих на скорость загрузки сайта
window.onReadyState = (e, t) => {
const a = ["loading", "interactive", "complete"],
o = a.slice(a.indexOf(e)),
n = () => o.includes(document.readyState);
n() ? t() : document.addEventListener("readystatechange", (() => n() && t()))
}
window.onReadyState("complete",function(){(function(m,e,t,r,i,k,a)..... })
//////////////////////////////////////////
//ACF Filter
<?php $GLOBALS['my_query_filters'] = array(
'field_1' => 'city',
'field_2' => 'vac'
);
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
<?php
// вывод дочерних рубрик и их записей на странице родительской рубрики
$parent_id = get_query_var('cat'); ?>
<?php
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
Проблема актуальна, можно решать таким образом:
<div class="top-menu" data-hover="hover-on-menu">
<a href="/to/your/url" data-hover="hover-on-link">Hello</a>
<script>
var touchHover = function() {
$('[data-hover]').click(function(e){
e.preventDefault();
@AntonLitvin
AntonLitvin / viewport.js
Last active October 18, 2020 13:31 — forked from delphinpro/viewport.js
Conditional meta viewport
/*
* Paste into the end <head>
*/
(function(){
var width = screen.width;
var oldViewport = document.querySelector('meta[name="viewport"]');
var viewport = document.createElement('meta');
viewport.setAttribute('name', 'viewport');
viewport.setAttribute('content', 'width=' + (width <= 640 ? '640' : 'device-width'));
document.head.replaceChild(viewport, oldViewport);
@AntonLitvin
AntonLitvin / woocommerce-give-subcat-list-items-their-own-ul.php
Created June 1, 2020 16:52 — forked from twoelevenjay/woocommerce-give-subcat-list-items-their-own-ul.php
Move WooCommerce subcategory list items into their own <ul> separate from the product <ul>.
<?php
/**
* Move WooCommerce subcategory list items into
* their own <ul> separate from the product <ul>.
*/
add_action( 'init', 'move_subcat_lis' );
function move_subcat_lis() {
// Remove the subcat <li>s from the old location.
@AntonLitvin
AntonLitvin / Modal
Last active January 10, 2020 10:37
Modal like bootstrap
<button class="open-modal-btn" data-target="modal">open modal</button>
<div class="modal" id="modal">
<div class="modal-inner">
<button type="button" class="close-modal-btn">&times;</button>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat ducimus tempora deserunt repellat architecto adipisci. </p>
</div>
</div>
@AntonLitvin
AntonLitvin / functions.php
Created February 9, 2019 07:54 — forked from vishalbasnet23/functions.php
Simple Ajax Pagination WP
<?php
add_action('wp_ajax_infinite_scroll_home', 'infinite_scroll_home', 0);
add_action('wp_ajax_nopriv_infinite_scroll_home', 'infinite_scroll_home');
function infinite_scroll_home() {
$exclude_posts_json = $_POST['exclude_posts'];
$exclude_posts = json_decode($exclude_posts_json);
$post_offset = $_POST['post_offset'];
$infinite_scroll_args = array( 'post_type'=>'post', 'posts_per_page'=> 2,'post__not_in' => $exclude_posts, 'offset' => $post_offset);
$infinite_scroll_query = new WP_Query( $infinite_scroll_args );
while( $infinite_scroll_query->have_posts() ) : $infinite_scroll_query->the_post();