Vladddosss
@Vladddosss

Что то намудрил с циклом WP_Query в чем ошибка?

Добрый день.

Пробую сделать виджет для Elementor по выводу постов, примерно такой:

5d5fe4bc8117b027688470.png

Что то не в порядке с циклом, обновляю страницу и страница белая, нагрузка на ЦП взлетает до небес, комментирую цикл, обновляю - страница работает.

Так вот, подскажите в чем ошибка.
spoiler

<?php
// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

use \Elementor\Group_Control_Image_Size as Group_Control_Image_Size;
use \Elementor\Controls_Manager as Controls_Manager;
use \Elementor\Group_Control_Typography as Group_Control_Typography;
use \Elementor\Group_Control_Css_Filter as Group_Control_Css_Filter;
use \Elementor\Group_Control_Box_Shadow as Group_Control_Box_Shadow;

/**
 * Elementor Drozd Widget.
 *
 * @since 1.0.0
 */
class Posts_Grid_1 extends \Elementor\Widget_Base {

	public function get_name() {
		return 'drozd-posts-grid-1';
	}

	public function get_title() {
		return __( 'Posts Grid Style 1', 'drozd' );
	}

	public function get_icon() {
		return 'eicon-call-to-action';
	}

	public function get_categories() {
		return [ 'drozd' ];
	}

	protected function _register_controls() {

		/*
		 * =======================================
		 * SETTINGS
		 * =======================================
		 */
		$this->start_controls_section(
			'posts_grid_1_settings',
			[
				'label' => __( 'Settings', 'drozd' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'heading_title',
			[
				'label' => __( 'Title', 'drozd' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$this->add_control(
			'widget_title',
			[
				'label' => __( 'Block Title', 'drozd' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'placeholder' => __( 'Enter your title', 'drozd' ),
				'label_block' => true,
			]
		);

		$this->add_control(
			'heading_posts',
			[
				'label' => __( 'Posts', 'drozd' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$this->add_control(
			'posts_number',
			array(
				'label'   => esc_html__( 'Number of posts to display:', 'drozd' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 4,
			)
		);

		$this->add_control(
			'offset_posts_number',
			array(
				'label' => esc_html__( 'Offset Posts:', 'drozd' ),
				'type'  => Controls_Manager::TEXT,
			)
		);

		$this->add_control(
			'heading_filter',
			[
				'label' => __( 'Filter', 'drozd' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$this->add_control(
			'display_type',
			array(
				'label'   => esc_html__( 'Display the posts from:', 'drozd' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'latest',
				'options' => array(
					'latest'     => esc_html__( 'Latest Posts', 'drozd' ),
					'categories' => esc_html__( 'Categories', 'drozd' ),
				),
			)
		);

		$this->add_control(
			'categories_selected',
			array(
				'label'     => esc_html__( 'Select categories:', 'drozd' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => drozd_elementor_categories(),
				'condition' => array(
					'display_type' => 'categories',
				),
			)
		);


		$this->end_controls_section();

		/*
		 * =======================================
		 * STYLE
		 * =======================================
		 */
		/* Style image */
		$this->start_controls_section(
			'section_image_box_image',
			[
				'label' => __( 'Widget Style', 'drozd' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'title_color',
			[
				'label' => __( 'Color', 'drozd' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'scheme' => [
					'type' => \Elementor\Scheme_Color::get_type(),
					'value' => \Elementor\Scheme_Color::COLOR_1,
				],
				'selectors' => [
					'{{WRAPPER}} .drozd-image-box-v1 .content .title' => 'color: {{VALUE}}',
				],
			]
		);

		$this->end_controls_section();

	}

	protected function render() {

		$settings = $this->get_settings_for_display();

		$posts_number        = $this->get_settings( 'posts_number' );
		$display_type        = $this->get_settings( 'display_type' );
		$offset_posts_number = $this->get_settings( 'offset_posts_number' );
		$categories_selected = $this->get_settings( 'categories_selected' );

		$args = array(
			'posts_per_page'      => $posts_number,
			'post_type'           => 'post',
			'ignore_sticky_posts' => true,
			'no_found_rows'       => true,
		);

		// Display from the category selected
		if ( 'categories' == $display_type ) {
			$args[ 'category__in' ] = $categories_selected;
		}

		// Offset the posts
		if ( ! empty( $offset_posts_number ) ) {
			$args[ 'offset' ] = $offset_posts_number;
		}

		// Start the WP_Query Object/Class
		$get_featured_posts = new \WP_Query( $args );
		?>
		<!--Posts Grid 1-->
		<div class="drozd-posts-grid-v1">

			<?php if ( ! empty( $settings['widget_title'] ) ) : ?>
				<div class="title-wrap">
					<h4 class="title"><?php echo $settings['widget_title']; ?></h4>
				</div>
			<?php endif; ?>

			<div class="dpg-wrap">
				<?php
				$count = 1;
				while ( $get_featured_posts->have_posts() ) :
					$get_featured_posts->the_posts();

					echo '<div class="column">';
					?>

					<div class="col-wrap">
						<?php
						if ( has_post_thumbnail() ) : ?>
							<a href="<?php the_permalink(); ?>">
								<?php the_post_thumbnail(); ?>
							</a>
						<?php endif; ?>

						<div class="meta-info">
							<div class="meta-align">
								<div class="tags">
									<a href="<?php echo esc_url( get_category_link( $post->ID ) ); ?>"><?php echo esc_html( get_the_category()->cat_name ); ?></a>
								</div>
							</div>
						</div>
					</div>

					<?php
					echo '</div>';

				$count ++;
				endwhile;
				
				// Restore original Post Data
				wp_reset_postdata();
				?>
			</div>
		</div>
		<!--Posts Grid 1-->
		<?php

	}

	protected function _content_template() {}

}

  • Вопрос задан
  • 57 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы