wefr
كذلك أخي عدل ملف functions.php ليصبح كما يلي من أجل ألا تظهر إلا مقالات الكاتب المعني:
<?php
function al_main_sidebar()
{
register_sidebar(
array(
'name' => 'main-aside',
'id' => 'main-aside',
'description' => 'sssssss',
'class' => 'sidee',
'before_widget' => '<div class= "all">',
'after_widget' => '</div>',
'before_title' => '<h1 class= "title">',
'after_title' => '</h1>'
)
);
}
add_action('widgets_init', 'al_main_sidebar');
// =================
/**
* Plugin Name: Latest Posts Widget
* Description: A widget to display the latest posts from a specific category.
* Version: 1.0
* Author: Your Name
*/
// Widget class.
class Latest_Posts_Widget extends WP_Widget {
// Constructor.
function __construct() {
parent::__construct(
'latest_posts_widget', // Base ID.
__('Latest Posts Widget', 'text_domain'), // Name.
array('description' => __('A widget to display the latest posts from a specific category.', 'text_domain'),) // Widget description.
);
}
// Widget output.
public function widget($args, $instance) {
if (is_author()) {
$queried_author = get_queried_object();
if ($queried_author instanceof WP_User) {
// استخدم معرف الكاتب الحالي
$author_id = $queried_author->ID;
} else {
return; // إذا لم نتمكن من الحصول على الكاتب، نخرج من الدالة
}
} else {
$author_id = null; // استخدم null للصفحات الأخرى
}
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
$category = !empty($instance['category']) ? $instance['category'] : '';
$number_posts = !empty($instance['number_posts']) ? intval($instance['number_posts']) : 5;
$show_title = isset($instance['show_title']) ? $instance['show_title'] : false;
$show_content = isset($instance['show_content']) ? $instance['show_content'] : false;
$show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : false;
$widget_class = !empty($instance['widget_class']) ? $instance['widget_class'] : '';
$query_args = array(
'post_type' => 'post',
'posts_per_page' => $number_posts,
'category_name' => $category,
'author' => $author_id, // استخدم معرف الكاتب إذا كنا في صفحة كاتب
);
$query = new WP_Query($query_args);
if ($query->have_posts()) {
echo '<ul class="' . esc_attr($widget_class) . '">';
while ($query->have_posts()) {
$query->the_post();
?>
<li>
<?php
if ($show_title) {
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
}
if ($show_featured_image && has_post_thumbnail()) {
?>
<div class="post-thumbnail">
<?php
echo '<a href="' . get_permalink() . '">' . get_the_post_thumbnail(get_the_ID(), 'thumbnail') . '</a>';
?>
</div>
<?php
}
if ($show_content) {
the_content();
}
?>
</li>
<?php
}
echo '</ul>';
} else {
echo '<h5>لا توجد مقالات</h5>';
}
wp_reset_postdata();
if (!empty($instance['more_text']) && !is_author()) {
echo '<li><a href="' . esc_url(get_term_link($category, 'category')) . '">' . $instance['more_text'] . '</a></li>';
}
echo $args['after_widget'];
}
// Widget form.
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : __('', 'text_domain');
$more_text = !empty($instance['more_text']) ? $instance['more_text'] : __('عرض المزيد', 'text_domain');
$category = !empty($instance['category']) ? $instance['category'] : '';
$number_posts = !empty($instance['number_posts']) ? intval($instance['number_posts']) : 5;
$show_title = isset($instance['show_title']) ? (bool) $instance['show_title'] : false;
$show_content = isset($instance['show_content']) ? (bool) $instance['show_content'] : false;
$show_featured_image = isset($instance['show_featured_image']) ? (bool) $instance['show_featured_image'] : false;
$widget_class = !empty($instance['widget_class']) ? $instance['widget_class'] : '';
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('العنوان :', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('التصنيف :', 'text_domain'); ?></label>
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat">
<option value=""><?php _e('(اختر التصنيف)', 'text_domain'); ?></option>
<?php
$categories = get_categories();
foreach ($categories as $cat) {
$selected = ($category == $cat->slug) ? ' selected="selected"' : '';
echo '<option value="' . esc_attr($cat->slug) . '"' . $selected . '>' . esc_html($cat->name) . '</option>';
}
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('number_posts'); ?>"><?php _e('عدد المنشورات :', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('number_posts'); ?>" name="<?php echo $this->get_field_name('number_posts'); ?>" type="number" min="1" value="<?php echo esc_attr($number_posts); ?>">
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_title); ?> id="<?php echo $this->get_field_id('show_title'); ?>" name="<?php echo $this->get_field_name('show_title'); ?>" />
<label for="<?php echo $this->get_field_id('show_title'); ?>"><?php _e('إظهار عنوان المقالة', 'text_domain'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_content); ?> id="<?php echo $this->get_field_id('show_content'); ?>" name="<?php echo $this->get_field_name('show_content'); ?>" />
<label for="<?php echo $this->get_field_id('show_content'); ?>"><?php _e('إظهار محتوى المقالة', 'text_domain'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($show_featured_image); ?> id="<?php echo $this->get_field_id('show_featured_image'); ?>" name="<?php echo $this->get_field_name('show_featured_image'); ?>" />
<label for="<?php echo $this->get_field_id('show_featured_image'); ?>"><?php _e('أظهر الصور المميزه للمقالة', 'text_domain'); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('widget_class'); ?>"><?php _e('Widget Class:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('widget_class'); ?>" name="<?php echo $this->get_field_name('widget_class'); ?>" type="text" value="<?php echo esc_attr($widget_class); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('more_text'); ?>"><?php _e('نص عرض المزيد :', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('more_text'); ?>" name="<?php echo $this->get_field_name('more_text'); ?>" type="text" value="<?php echo esc_attr($more_text); ?>">
</p>
<?php
}
// Widget update.
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
$instance['more_text'] = (!empty($new_instance['more_text'])) ? strip_tags($new_instance['more_text']) : '';
$instance['category'] = (!empty($new_instance['category'])) ? $new_instance['category'] : '';
$instance['number_posts'] = (!empty($new_instance['number_posts'])) ? intval($new_instance['number_posts']) : '';
$instance['show_title'] = isset($new_instance['show_title']) ? (bool) $new_instance['show_title'] : false;
$instance['show_content'] = isset($new_instance['show_content']) ? (bool) $new_instance['show_content'] : false;
$instance['show_featured_image'] = isset($new_instance['show_featured_image']) ? (bool) $new_instance['show_featured_image'] : false;
$instance['widget_class'] = !empty($new_instance['widget_class']) ? $new_instance['widget_class'] : '';
return $instance;
}
}
// Register widget.
function register_latest_posts_widget() {
register_widget('Latest_Posts_Widget');
}
add_action('widgets_init', 'register_latest_posts_widget');