ابو زايد العبد

زيزوومي جديد
إنضم
15 يوليو 2024
المشاركات
69
مستوى التفاعل
45
النقاط
60
غير متصل
السلام عليكم

ابحث عن كود للوردبريس لاظهار المحتوى الاكثر زياره خلال 24 ساعه
 

توقيع : ابو زايد العبد
لعرض العدد فقط استخدم هذا ال plugin

يجب عليك تسجيل الدخول او تسجيل لمشاهدة الرابط المخفي

أما لعرض العدد والأسماء في ال Frontend فاستخدم

يجب عليك تسجيل الدخول او تسجيل لمشاهدة الرابط المخفي
أو
يجب عليك تسجيل الدخول او تسجيل لمشاهدة الرابط المخفي
ابحث عن كود للوردبريس لاظهار المحتوى الاكثر زياره خلال 24 ساعه
 
توقيع : ابو زايد العبد
اعلم ان الموضوع قديم ولكن احببت ان شارك بهذا الكود للفائدة

قم بوضع هذا الكود في ملف functions.php

PHP:
function display_popular_posts($num_posts = 5) {
    $args = [
        'post_type'      => 'post',
        'posts_per_page' => $num_posts,
        'meta_key'       => 'post_views_count',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
    ];

    $popular_posts = new WP_Query($args);

    if ($popular_posts->have_posts()) {
        echo '<ul class="popular-posts">';
        while ($popular_posts->have_posts()) {
            $popular_posts->the_post();
            echo '<li>';
            echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
            echo ' - ' . get_post_meta(get_the_ID(), 'post_views_count', true) . ' views';
            echo '</li>';
        }
        echo '</ul>';
    }

    wp_reset_postdata();
}

function popular_posts_shortcode($atts) {
    $atts = shortcode_atts(['num' => 5], $atts, 'popular_posts');
    ob_start();
    display_popular_posts($atts['num']);
    return ob_get_clean();
}
add_shortcode('popular_posts', 'popular_posts_shortcode');


وبعدها قم باستخدام هذا الشورت كود في اي مكان تريد

HTML:
[popular_posts num="5"]

مع تغيير رقم 5 الى عدد المواضيع التي تريد عرضها.

احترامي وتقديري
اخوك
شقاوي
 
اعلم ان الموضوع قديم ولكن احببت ان شارك بهذا الكود للفائدة

قم بوضع هذا الكود في ملف functions.php

PHP:
function display_popular_posts($num_posts = 5) {
    $args = [
        'post_type'      => 'post',
        'posts_per_page' => $num_posts,
        'meta_key'       => 'post_views_count',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
    ];

    $popular_posts = new WP_Query($args);

    if ($popular_posts->have_posts()) {
        echo '<ul class="popular-posts">';
        while ($popular_posts->have_posts()) {
            $popular_posts->the_post();
            echo '<li>';
            echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
            echo ' - ' . get_post_meta(get_the_ID(), 'post_views_count', true) . ' views';
            echo '</li>';
        }
        echo '</ul>';
    }

    wp_reset_postdata();
}

function popular_posts_shortcode($atts) {
    $atts = shortcode_atts(['num' => 5], $atts, 'popular_posts');
    ob_start();
    display_popular_posts($atts['num']);
    return ob_get_clean();
}
add_shortcode('popular_posts', 'popular_posts_shortcode');


وبعدها قم باستخدام هذا الشورت كود في اي مكان تريد

HTML:
[popular_posts num="5"]

مع تغيير رقم 5 الى عدد المواضيع التي تريد عرضها.

احترامي وتقديري
اخوك
شقاوي

استاذ تركي هل الشورت كود هذا للاكثر مشاهده طوال الوقت ام خلال 24 ساعه

اريد الى يكون خلال اليوم وجزاكم الله خير
 
توقيع : النائب العبد
استاذ تركي هل الشورت كود هذا للاكثر مشاهده طوال الوقت ام خلال 24 ساعه

اريد الى يكون خلال اليوم وجزاكم الله خير

هذا الشورت كود سيعرض الأكثر مشاهدة طوال الوقت.

لعرض الأكثر مشاهدة في آخر 24 ساعة ستحتاج لإضافة هذا الكود في ال Arguments ($args)


كود:
'date_query' => array(
        array(
            'after' => '24 hours ago'
        )
 
حياك الله اخوي @Elabd اذا تحتاجها ﻻخر 24 ساعة استبدل الكود السابق بهذا الكود

PHP:
// Track Post Views with Timestamp
function track_post_views($post_id) {
    if (!is_single() || empty($post_id)) {
        return;
    }

    $views = get_post_meta($post_id, 'post_views_count', true);
    $timestamps = get_post_meta($post_id, 'post_views_timestamps', true);

    if (empty($views)) {
        $views = 0;
    }

    if (!is_array($timestamps)) {
        $timestamps = [];
    }

    $views++;
    $timestamps[] = time(); // Record the current timestamp

    update_post_meta($post_id, 'post_views_count', $views);
    update_post_meta($post_id, 'post_views_timestamps', $timestamps);
}
add_action('wp_head', function() {
    if (is_single()) {
        track_post_views(get_the_ID());
    }
});

// Count Views in the Last 24 Hours
function get_recent_views_count($post_id) {
    $timestamps = get_post_meta($post_id, 'post_views_timestamps', true);
    if (empty($timestamps) || !is_array($timestamps)) {
        return 0;
    }

    $last_24_hours = time() - (24 * 60 * 60); // Timestamp 24 hours ago
    $recent_views = array_filter($timestamps, function($timestamp) use ($last_24_hours) {
        return $timestamp >= $last_24_hours;
    });

    return count($recent_views);
}

function display_popular_posts_last_24_hours($num_posts = 5) {
    $args = [
        'post_type'      => 'post',
        'posts_per_page' => -1, // Fetch all posts to filter manually
    ];

    $all_posts = new WP_Query($args);
    $popular_posts = [];

    if ($all_posts->have_posts()) {
        while ($all_posts->have_posts()) {
            $all_posts->the_post();
            $recent_views = get_recent_views_count(get_the_ID());
            if ($recent_views > 0) {
                $popular_posts[get_the_ID()] = $recent_views;
            }
        }
    }

    // Sort posts by views in descending order
    arsort($popular_posts);

    // Display the top $num_posts
    echo '<ul class="popular-posts">';
    $count = 0;
    foreach ($popular_posts as $post_id => $views) {
        if ($count >= $num_posts) {
            break;
        }
        echo '<li>';
        echo '<a href="' . get_permalink($post_id) . '">' . get_the_title($post_id) . '</a>';
        echo ' - ' . $views . ' views';
        echo '</li>';
        $count++;
    }
    echo '</ul>';

    wp_reset_postdata();
}

// Use the function in a template
display_popular_posts_last_24_hours(5);

function popular_posts_last_24_hours_shortcode($atts) {
    $atts = shortcode_atts(['num' => 5], $atts, 'popular_posts_last_24_hours');
    ob_start();
    display_popular_posts_last_24_hours($atts['num']);
    return ob_get_clean();
}
add_shortcode('popular_posts_last_24_hours', 'popular_posts_last_24_hours_shortcode');

واستخدم الشورت كود هذا بدل السابق

كود:
[popular_posts_last_24_hours num="5"]

مع تغيير رقم 5 الى عدد المواضيع الذي تريد

ونصيحة جانبية لكل من يستخدم الورد بريس بدلا ً من اضافة اكواد التعديل داخل ملف functions.php

اﻻفضل ان تقوم بانشاء ملف جديد لكل تعديل على حده وتقوم بعمل انكلود للملف الجديد في ملف functions.php

ﻻنه في المستقبل اذا كثرت اﻻكواد وسبب احد هذه اﻻكواد مشكلة فتتبع المشكلة سوف يسبب لك الصداع

احترامي وتقديري
اخوكم
شقاوي
 
حياك الله اخوي @Elabd اذا تحتاجها ﻻخر 24 ساعة استبدل الكود السابق بهذا الكود

PHP:
// Track Post Views with Timestamp
function track_post_views($post_id) {
    if (!is_single() || empty($post_id)) {
        return;
    }

    $views = get_post_meta($post_id, 'post_views_count', true);
    $timestamps = get_post_meta($post_id, 'post_views_timestamps', true);

    if (empty($views)) {
        $views = 0;
    }

    if (!is_array($timestamps)) {
        $timestamps = [];
    }

    $views++;
    $timestamps[] = time(); // Record the current timestamp

    update_post_meta($post_id, 'post_views_count', $views);
    update_post_meta($post_id, 'post_views_timestamps', $timestamps);
}
add_action('wp_head', function() {
    if (is_single()) {
        track_post_views(get_the_ID());
    }
});

// Count Views in the Last 24 Hours
function get_recent_views_count($post_id) {
    $timestamps = get_post_meta($post_id, 'post_views_timestamps', true);
    if (empty($timestamps) || !is_array($timestamps)) {
        return 0;
    }

    $last_24_hours = time() - (24 * 60 * 60); // Timestamp 24 hours ago
    $recent_views = array_filter($timestamps, function($timestamp) use ($last_24_hours) {
        return $timestamp >= $last_24_hours;
    });

    return count($recent_views);
}

function display_popular_posts_last_24_hours($num_posts = 5) {
    $args = [
        'post_type'      => 'post',
        'posts_per_page' => -1, // Fetch all posts to filter manually
    ];

    $all_posts = new WP_Query($args);
    $popular_posts = [];

    if ($all_posts->have_posts()) {
        while ($all_posts->have_posts()) {
            $all_posts->the_post();
            $recent_views = get_recent_views_count(get_the_ID());
            if ($recent_views > 0) {
                $popular_posts[get_the_ID()] = $recent_views;
            }
        }
    }

    // Sort posts by views in descending order
    arsort($popular_posts);

    // Display the top $num_posts
    echo '<ul class="popular-posts">';
    $count = 0;
    foreach ($popular_posts as $post_id => $views) {
        if ($count >= $num_posts) {
            break;
        }
        echo '<li>';
        echo '<a href="' . get_permalink($post_id) . '">' . get_the_title($post_id) . '</a>';
        echo ' - ' . $views . ' views';
        echo '</li>';
        $count++;
    }
    echo '</ul>';

    wp_reset_postdata();
}

// Use the function in a template
display_popular_posts_last_24_hours(5);

function popular_posts_last_24_hours_shortcode($atts) {
    $atts = shortcode_atts(['num' => 5], $atts, 'popular_posts_last_24_hours');
    ob_start();
    display_popular_posts_last_24_hours($atts['num']);
    return ob_get_clean();
}
add_shortcode('popular_posts_last_24_hours', 'popular_posts_last_24_hours_shortcode');

واستخدم الشورت كود هذا بدل السابق

كود:
[popular_posts_last_24_hours num="5"]

مع تغيير رقم 5 الى عدد المواضيع الذي تريد

ونصيحة جانبية لكل من يستخدم الورد بريس بدلا ً من اضافة اكواد التعديل داخل ملف functions.php

اﻻفضل ان تقوم بانشاء ملف جديد لكل تعديل على حده وتقوم بعمل انكلود للملف الجديد في ملف functions.php

ﻻنه في المستقبل اذا كثرت اﻻكواد وسبب احد هذه اﻻكواد مشكلة فتتبع المشكلة سوف يسبب لك الصداع

احترامي وتقديري
اخوكم
شقاوي

استاذنا هل يمكن اضيفها كشورت كود ف الويدجات مباشره بدون تعديل ف ملف
functions.php

وجزاكم الله خير على اهتمامك وردك
 
توقيع : النائب العبد
استاذنا هل يمكن اضيفها كشورت كود ف الويدجات مباشره بدون تعديل ف ملف
functions.php

وجزاكم الله خير على اهتمامك وردك

للاسف ﻻبد من اضافة الكود الى ملف functions ﻻنه بدون هالكود الشروت كود لن يعمل
 
عودة
أعلى