%PDF- %PDF-
Direktori : /var/www/swedenbusiness/wp-content/themes/swissbusiness/ |
Current File : //var/www/swedenbusiness/wp-content/themes/swissbusiness/functions.php |
<?php /** * swissbusiness functions and definitions * * @link https://developer.wordpress.org/themes/basics/theme-functions/ * * @package swissbusiness */ require_once('inc/constants.php'); if (!defined('_S_VERSION')) { // Replace the version number of the theme on each release. define('_S_VERSION', '1.0.0'); } if (!function_exists('swissbusiness_setup')) : /** * Sets up theme defaults and registers support for various WordPress features. * * Note that this function is hooked into the after_setup_theme hook, which * runs before the init hook. The init hook is too late for some features, such * as indicating support for post thumbnails. */ function swissbusiness_setup() { // Add default posts and comments RSS feed links to head. add_theme_support('automatic-feed-links'); /* * Let WordPress manage the document title. * By adding theme support, we declare that this theme does not use a * hard-coded <title> tag in the document head, and expect WordPress to * provide it for us. */ add_theme_support('title-tag'); /* * Enable support for Post Thumbnails on posts and pages. * * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/ */ add_theme_support('post-thumbnails'); // This theme uses wp_nav_menu() in one location. register_nav_menus( array( 'menu-1' => esc_html__('Primary', 'swissbusiness'), ) ); /* * Switch default core markup for search form, comment form, and comments * to output valid HTML5. */ add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script', ) ); // Add theme support for selective refresh for widgets. add_theme_support('customize-selective-refresh-widgets'); /** * Add support for core custom logo. * * @link https://codex.wordpress.org/Theme_Logo */ add_theme_support( 'custom-logo', array( 'height' => 250, 'width' => 250, 'flex-width' => true, 'flex-height' => true, ) ); } endif; add_action('after_setup_theme', 'swissbusiness_setup'); /** * Register widget area. * * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar */ // function swissbusiness_widgets_init() { // register_sidebar( // array( // 'name' => esc_html__( 'Sidebar', 'swissbusiness' ), // 'id' => 'sidebar-1', // 'description' => esc_html__( 'Add widgets here.', 'swissbusiness' ), // 'before_widget' => '<section id="%1$s" class="widget %2$s">', // 'after_widget' => '</section>', // 'before_title' => '<h2 class="widget-title">', // 'after_title' => '</h2>', // ) // ); // } // add_action( 'widgets_init', 'swissbusiness_widgets_init' ); /** * Enqueue scripts and styles. */ function swissbusiness_scripts() { // jquery wp_enqueue_script('jquery', get_template_directory_uri() . '/assets/libs/jquery/jquery.min.js', array(), null, true); wp_enqueue_script('jquery.mask', get_template_directory_uri() . '/assets/libs/jquery-mask/jquery.mask.min.js', array('jquery'), _S_VERSION, true); //magnific-popup wp_enqueue_style('magnific-popup', get_template_directory_uri() . '/assets/libs/magnific-popup/magnific-popup.css', array(), _S_VERSION); wp_enqueue_script('magnific-popup', get_template_directory_uri() . '/assets/libs/magnific-popup/jquery.magnific-popup.min.js', array('jquery'), _S_VERSION); //Masonry wp_enqueue_script('masonry', get_template_directory_uri() . '/assets/libs/masonry/masonry.pkgd.min.js', array('jquery'), _S_VERSION, true); // wp_enqueue_style('masonry', 'https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js', array('jquery') ); // Slick wp_enqueue_style('slick', get_template_directory_uri() . '/assets/libs/slick/slick.css', array(), _S_VERSION); wp_enqueue_script('slick', get_template_directory_uri() . '/assets/libs/slick/slick.min.js', array('jquery'), _S_VERSION); // styles wp_enqueue_style('main-style', get_template_directory_uri() . '/assets/css/main.css', array(), time()); wp_enqueue_style('custom-style', get_stylesheet_uri(), array(), time()); // script wp_enqueue_script('main-js', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), time(), true); } add_action('wp_enqueue_scripts', 'swissbusiness_scripts'); remove_filter('pre_user_description', 'wp_filter_kses'); add_image_size('article-thumb', 680, 400, true); //Укоротить текст // add_filter('excerpt_length', function () { // return 15; // }); // add_filter('excerpt_more', function ($more) { // return '...'; // }); function excerpt($limit) { $excerpt = explode(' ', get_the_excerpt(), $limit); if (count($excerpt) >= $limit) { array_pop($excerpt); $excerpt = implode(" ", $excerpt) . ''; } else { $excerpt = implode(" ", $excerpt); } $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt); return $excerpt; } function content($limit) { $content = explode(' ', get_the_content(), $limit); if (count($content) >= $limit) { array_pop($content); $content = implode(" ", $content) . '...'; } else { $content = implode(" ", $content); } $content = preg_replace('/\[.+\]/', '', $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); return $content; } add_action('wp_ajax_savedArticles', 'savedArticles'); add_action('wp_ajax_nopriv_savedArticles', 'savedArticles'); function savedArticles() { $template = 'template-parts/saved-articles'; get_template_part($template); die(); } add_action('wp_ajax_getSavedSize', 'getSavedSize'); add_action('wp_ajax_nopriv_getSavedSize', 'getSavedSize'); function getSavedSize() { $postsId = $_POST['postsId']; $args = array( 'post_type' => 'articles', 'posts_per_page' => -1, ); $query = new WP_Query($args); $cnt = 0; while ($query->have_posts()) { $query->the_post(); if (in_array(get_the_ID(), $postsId)) { $cnt++; } } echo $cnt; die(-1); } add_action('wp_ajax_loadMore', 'loadMore'); add_action('wp_ajax_nopriv_loadMore', 'loadMore'); function loadMore() { $count = intval($_POST['count']); $step = intval($_POST['step']); $type = $_POST['type']; $args = [ 'post_type' => "articles", 'post_status' => 'publish', 'posts_per_page' => $step, 'post_parent' => 0, 'offset' => $count, ]; if(array_key_exists('cat', $_POST)) { $cat = intval($_POST['cat']); $args['cat'] = $cat; $catObj = get_category($cat); $area_1 = get_field('area_1', $catObj); $area_2 = get_field('area_2', $catObj); $area_3 = get_field('area_3', $catObj); } $query = new WP_Query($args); // PC::debug($_POST, '$_POST'); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); $cat = get_the_terms(get_the_id(), 'category')[0]; $postCount = $query->current_post + 1; $params = ['cat' => $cat]; $posts_quantity = $count - intval(get_option('posts_per_page')); if($posts_quantity === 0 || ($posts_quantity % 2 === 0 && $posts_quantity % 4 === 0)) { if($area_1) { get_template_part('template-parts/banner', '', $area_1); } } if( $posts_quantity % 2 === 0 && $posts_quantity % 4 === 2 ) { if($area_2) { get_template_part('template-parts/banner', '', $area_2); } } $children = new WP_Query([ 'post_type' => 'articles', 'post_status' => 'publish', 'posts_per_page' => -1, 'post_parent' => get_the_id() ]); if($children->have_posts()) { $params['children'] = $children; get_template_part('template-parts/large-post_articleserie', '', $params); } else { get_template_part('template-parts/large-post_v2', '', $params); } endwhile; endif; wp_reset_postdata(); die(-1); } add_action('acf/init', 'my_acf_op_init'); function my_acf_op_init() { // Check function exists. if (function_exists('acf_add_options_page')) { // Register options page. acf_add_options_page(array( 'page_title' => __('General settings'), 'menu_title' => __('General settings'), 'menu_slug' => 'theme-general-settings', 'capability' => 'edit_posts', 'redirect' => false )); } } add_action('after_setup_theme', 'theme_register_nav_menu'); function theme_register_nav_menu() { register_nav_menu('header_1', 'Explore (header)'); register_nav_menu('header_2', 'Kategorier (header)'); register_nav_menu('header_3', 'Follow-us (header)'); register_nav_menu('footer_1', 'Explore (footer)'); register_nav_menu('footer_2', 'Follow-us (footer)'); } function custom_posttype_query( $query ) { if( (is_category() || is_tag() || is_author()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) && !is_admin() && $query->is_main_query() ) { $query->set( 'post_type', 'articles'); $query->set( 'post_parent', 0); } return $query; } add_filter( 'pre_get_posts', 'custom_posttype_query' );