You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
cosmopet-architecture/wp-content/themes/cosmopet/modules/blog/module-ajax-controller.php

95 lines
2.7 KiB

<?php
// echo '<pre>';
// var_dump('author');
// print_r('author');
// echo '</pre>';
// die();
include_module('blog');
function get_posts_by_page_and_category($paged = 1, $category = '') {
$args = [
'post_type' => 'post',
'posts_per_page' => 9, // Adjust the number of posts per page
'paged' => intval($paged) + 1,
'post_status' => 'publish', // Только опубликованные записи
];
if (!empty($category)) {
$args['tax_query'] = [
[
'taxonomy' => 'category',
'field' => 'slug', // or 'term_id', 'name' depending on how you identify categories
'terms' => $category,
],
];
}
$q = new WP_Query($args);
return new Timber\PostQuery($q);
}
function ajax_load_blog_posts() {
$page_num = isset($_POST['page_num']) ? sanitize_text_field($_POST['page_num']) : '';
$context = Timber::context();
$category = $_POST['category'];
$context['posts'] = get_posts_by_page_and_category($page_num, $category);
$html = Timber::compile('/blog/news-list.twig', $context);
echo $html;
wp_die();
}
add_action('wp_ajax_load_blog_posts', 'ajax_load_blog_posts');
add_action('wp_ajax_nopriv_load_blog_posts', 'ajax_load_blog_posts');
function ajax_front_page_get_blog_posts() {
$context = Timber::context();
$category = $_POST['category'];
$args = [
'post_type' => 'post',
'posts_per_page' => 5, // Adjust the number of posts per page
'paged' => intval($paged) + 1,
'post_status' => 'publish', // Только опубликованные записи
];
if (!empty($category)) {
$args['tax_query'] = [
[
'taxonomy' => 'category',
'field' => 'slug', // or 'term_id', 'name' depending on how you identify categories
'terms' => $category,
],
];
}
$featured_query = new WP_Query($args);
$f_query = array();
$i = 0;
foreach($featured_query->posts as $item){
if ($i > 0){
$f_query[] = Timber::get_post($item, 'BlogPost');
}
else{
$featured_post = Timber::get_post($item, 'BlogPost');
}
$i++;
}
$context['blog_posts'] = $f_query;
$context['featured_post'] = $featured_post;
$html = Timber::compile('/blog/blog-front-page-post-list_block.twig', $context);
echo $html;
wp_die();
}
add_action('wp_ajax_front_page_get_blog_posts', 'ajax_front_page_get_blog_posts');
add_action('wp_ajax_nopriv_front_page_get_blog_posts', 'ajax_front_page_get_blog_posts');