Created
June 25, 2025 06:18
-
-
Save mehrshaddarzi/7daf6ab96d6e60fd2e488a21db5ab6f9 to your computer and use it in GitHub Desktop.
elemntor_widget.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Elementor\Core\DynamicTags\Tag; | |
class Custom_Meta_Fields_Tag extends \Elementor\Core\DynamicTags\Tag { | |
public function get_name() { | |
return 'custom-meta-fields'; | |
} | |
public function get_title() { | |
return 'متاهای سفارشی'; | |
} | |
public function get_group() { | |
return 'post'; // گروه Post | |
} | |
public function get_categories() { | |
return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ]; | |
} | |
protected function register_controls() { | |
$this->add_control( | |
'meta_key', | |
[ | |
'label' => 'کلید متا', | |
'type' => \Elementor\Controls_Manager::SELECT, | |
'options' => $this->get_meta_keys(), | |
] | |
); | |
} | |
private function get_meta_keys() { | |
// اینجا کلیدهای متا دلخواه خودت رو مشخص کن | |
return [ | |
'my_custom_field_1' => 'فیلد سفارشی ۱', | |
'my_custom_field_2' => 'فیلد سفارشی ۲', | |
// ... کلیدهای بیشتر ... | |
]; | |
} | |
public function render() { | |
$meta_key = $this->get_settings( 'meta_key' ); | |
if ( ! $meta_key ) { | |
return; | |
} | |
echo get_post_meta( get_the_ID(), $meta_key, true ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<?php | |
use Elementor\Widget_Base; | |
use Elementor\Controls_Manager; | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
class Widget_Latest_Products extends Widget_Base { | |
public function get_name() { | |
return 'latest_products'; | |
} | |
public function get_title() { | |
return 'آخرین محصولات'; | |
} | |
public function get_icon() { | |
return 'eicon-products'; | |
} | |
public function get_categories() { | |
return [ 'general' ]; | |
} | |
protected function register_controls() { | |
$this->start_controls_section( | |
'section_content', | |
[ | |
'label' => 'تنظیمات', | |
] | |
); | |
$this->add_control( | |
'product_count', | |
[ | |
'label' => 'تعداد محصولات', | |
'type' => Controls_Manager::NUMBER, | |
'min' => 1, | |
'max' => 50, | |
'default' => 4, | |
] | |
); | |
$this->add_control( | |
'background_color', | |
[ | |
'label' => 'رنگ پسزمینه', | |
'type' => Controls_Manager::COLOR, | |
'default' => '#f9f9f9', | |
'selectors' => [ | |
'{{WRAPPER}} .custom-products-loop' => 'background-color: {{VALUE}};', | |
], | |
] | |
); | |
// کنترل انتخاب تمپلیت المنتور (template_id) | |
$this->add_control( | |
'template_id', | |
[ | |
'label' => 'انتخاب قالب المنتور برای هر محصول', | |
'type' => Controls_Manager::SELECT2, | |
'options' => $this->get_elementor_templates(), | |
'label_block' => true, | |
'description' => 'اگر قالبی انتخاب شود، هر محصول با آن قالب نمایش داده میشود.', | |
] | |
); | |
$this->end_controls_section(); | |
} | |
// تابع کمکی برای گرفتن لیست تمپلیتهای المنتور (پست تایپ elementor_library) | |
private function get_elementor_templates() { | |
$templates = []; | |
$args = [ | |
'post_type' => 'elementor_library', | |
'posts_per_page' => -1, | |
'post_status' => 'publish', | |
]; | |
$query = new WP_Query($args); | |
if ($query->have_posts()) { | |
foreach ($query->posts as $post) { | |
$templates[$post->ID] = $post->post_title; | |
} | |
} | |
wp_reset_postdata(); | |
return $templates; | |
} | |
protected function render() { | |
$settings = $this->get_settings_for_display(); | |
$count = $settings['product_count']; | |
$template_id = $settings['template_id']; | |
$args = [ | |
'post_type' => 'product', | |
'posts_per_page' => $count, | |
]; | |
$query = new WP_Query($args); | |
if ( $query->have_posts() ) { | |
echo '<div class="custom-products-loop" style="padding: 20px;">'; | |
if ( $template_id ) { | |
// رندر با قالب المنتور | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
global $post; | |
$current_post = $post; | |
echo Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id ); | |
$post = $current_post; // برگرداندن global post | |
} | |
} else { | |
// رندر ساده اگر قالب انتخاب نشده | |
echo '<div class="product-grid" style="display: flex; gap: 20px; flex-wrap: wrap;">'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
echo '<div class="product-item" style="border: 1px solid #ddd; padding: 15px; width: 200px;">'; | |
if ( has_post_thumbnail() ) { | |
the_post_thumbnail('medium'); | |
} | |
echo '<h4>' . get_the_title() . '</h4>'; | |
echo '</div>'; | |
} | |
echo '</div>'; | |
} | |
echo '</div>'; | |
wp_reset_postdata(); | |
} else { | |
echo '<p>محصولی یافت نشد.</p>'; | |
} | |
} | |
} | |
// Register | |
function register_custom_elementor_widgets( $widgets_manager ) { | |
require_once get_template_directory() . '/elementor-widgets/latest-products-widget.php'; | |
$widgets_manager->register( new \Widget_Latest_Products() ); | |
} | |
add_action( 'elementor/widgets/register', 'register_custom_elementor_widgets' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment