• Resolved ozgurgedikli

    (@ozgurgedikli)


    I am trying to show the matches in a different marquee text toner in the Alchemists theme. However, I am not getting the score information. Does anyone have any ideas?
    Where am I going wrong

    Match day range filter is filtered as 4 days before and after OK
    Match published – scheduled information OK
    Match date and time information OK
    Getting the match score is UNABLE

    Display with shortcode [matches_ticker title=”Matches” posts_per_page=”50″]

    Match Published <marquee> 2.League: September 8, 2024 Team1 4 – 3 Team2
    Match Scheduled <marquee> 2.League: September 11, 2024 Team2 vs Team1 18:30

    <?php
    // Shortcode attributes
    $atts = vc_map_get_attributes( $this->getShortcode(), $atts );
    extract( $atts );

    // Set class for marquee wrapper
    $css_class = 'marquee-wrapper' . vc_shortcode_custom_css_class( $css, ' ' ) . $this->getExtraClass( $el_class );

    // Query SportPress events (matches)
    $args = array(
    'post_type' => 'sp_event',
    'post_status' => array( 'publish', 'future' ),
    'posts_per_page' => 10,
    'date_query' => array(
    array(
    'after' => date('Y-m-d', strtotime('-4 days')), // 4 gün önce
    'before' => date('Y-m-d', strtotime('+4 days')), // 4 gün sonrası
    'inclusive' => true,
    'type' => 'DATE',
    ),
    ),
    );

    $events_query = new WP_Query($args);
    ?>

    <!-- Matches Ticker -->
    <div class="<?php echo esc_attr($css_class); ?>">
    <div class="container">

    <?php if ( ! empty( $title ) ) : ?>
    <div class="marquee-label">
    <?php echo esc_html( $title ); ?>
    </div>
    <?php endif; ?>

    <?php if ( $events_query->have_posts() ) : ?>
    <div class="marquee" data-gap="10" data-duplicated="false">
    <ul class="posts posts--inline">
    <?php
    $is_first_item = true; // İlk elemanı kontrol etmek için bir değişken oluşturuyoruz.
    while ( $events_query->have_posts() ) : $events_query->the_post(); ?>
    <?php
    // Event ID'yi al
    $event_id = get_the_ID();

    // Takım bilgilerini alalım
    $teams = sp_get_teams($event_id); // Takımların ID'lerini al

    if (is_array($teams) && count($teams) > 1) {
    // Takım 1 ve Takım 2 bilgilerini al
    $team1_id = $teams[0];
    $team2_id = $teams[1];

    // Takım isimlerini çek
    $team1_name = get_the_title($team1_id);
    $team2_name = get_the_title($team2_id);
    } else {
    // Eğer takımlar eksikse varsayılan "Bilinmiyor"
    $team1_name = $team2_name = 'Bilinmiyor';
    }

    // Maç skoru alalım (ana sonuçları almak için sp_get_main_results kullanıyoruz)
    $main_results = sp_get_main_results( $event_id );

    // Skorları kontrol et
    $home_score = isset($main_results['home']['goals']) ? $main_results['home']['goals'] : null;
    $away_score = isset($main_results['away']['goals']) ? $main_results['away']['goals'] : null;

    // Eğer skorlar boş ise, outcome verisini kontrol edelim
    if ($home_score === null || $away_score === null) {
    // Outcome'ı alalım
    $outcome = get_post_meta($event_id, 'sp_outcome', true);

    // Outcome verisi varsa, sonucu "Galibiyet" "Mağlubiyet" veya "Beraberlik" olarak göster
    if (!empty($outcome)) {
    if ($outcome == 'win') {
    $home_score = 'Galibiyet';
    $away_score = '';
    } elseif ($outcome == 'loss') {
    $home_score = 'Mağlubiyet';
    $away_score = '';
    } elseif ($outcome == 'draw') {
    $home_score = 'Beraberlik';
    $away_score = '';
    } else {
    $home_score = $away_score = 'Bilinmiyor';
    }
    } else {
    $home_score = $away_score = 'Bilinmiyor';
    }
    }

    // Maç saati UTC+3 olarak ayarlanacak
    $match_time_utc = get_post_time( 'H:i', true, $event_id );
    $match_time_utc3 = strtotime($match_time_utc) + (3 * 3600); // 3 saat ekleyerek UTC+3 yapıyoruz

    // Formatlı saat UTC+3'e göre
    $formatted_time = !empty($match_time_utc) ? date('H:i', $match_time_utc3) : 'Bilinmiyor';

    // Maç tarihi
    $date_html = get_post_time( 'Y-m-d', false, $event_id );

    // Maç tarihi varsa formatla, yoksa "Bilinmiyor" kullan
    $formatted_date = !empty($date_html) ? date_i18n('j F Y', strtotime($date_html)) : 'Bilinmiyor'; // Türkçe tarih formatı

    // Lig adı
    $leagues = wp_get_post_terms($event_id, 'sp_league', array('fields' => 'names'));
    $league_name = !empty($leagues) ? $leagues[0] : 'Lig Bilinmiyor';

    // Maçın durumu: Yayınlanmış mı, gelecekte mi?
    $post_status = get_post_status($event_id);

    // Şu anki zamanı alalım
    $current_time = current_time('Y-m-d H:i');

    // Geçmiş Maç Kontrolü: Oynamış maçları ve skorları ayıralım
    if ($post_status == 'publish' && !empty($home_score) && !empty($away_score) && $date_html < date('Y-m-d')) {
    // Geçmiş maç ise sonuçları göster
    $match_text = esc_html($formatted_date . ' - ' . $team1_name . ' ' . $home_score . ' - ' . $away_score . ' ' . $team2_name);
    }
    // Eğer maç bugünün tarihine sahip ve saat şu anki saatten daha sonraysa (yani oynanmamışsa)
    elseif ($post_status == 'publish' && ($date_html == date('Y-m-d')) && ($match_time_utc3 > strtotime($current_time))) {
    $match_text = esc_html($formatted_date . ' - ' . $team1_name . ' vs ' . $team2_name . ' - ' . $formatted_time);
    }
    // Eğer maç geçmişte oynanmışsa ve skoru eksikse, outcome ile durumu göster
    elseif ( $post_status == 'publish' && ($date_html < date('Y-m-d')) && (empty($home_score) || empty($away_score)) ) {
    $outcome = get_post_meta($event_id, 'sp_outcome', true);
    if ($outcome == 'win') {
    $home_score = 'Galibiyet';
    $away_score = '';
    } elseif ($outcome == 'loss') {
    $home_score = 'Mağlubiyet';
    $away_score = '';
    } elseif ($outcome == 'draw') {
    $home_score = 'Beraberlik';
    $away_score = '';
    }
    $match_text = esc_html($formatted_date . ' - ' . $team1_name . ' ' . $home_score . ' - ' . $away_score . ' ' . $team2_name);
    }
    // Gelecek maçlar: Tarih ve saat ile göster
    elseif ( $post_status == 'future' || $date_html > date('Y-m-d') ) {
    // Gelecek maçsa "vs" ve saat göster
    $match_text = esc_html($formatted_date . ' - ' . $team1_name . ' vs ' . $team2_name . ' - ' . $formatted_time);
    }
    // Diğer durumlar için "Bilinmiyor"
    else {
    $match_text = esc_html($formatted_date . ' - ' . $team1_name . ' vs ' . $team2_name . ' Bilinmiyor');
    }

    // Separator ekleme işlemi
    if ( !$is_first_item ) {
    echo '<li class="posts__item separator"> | </li>'; // Separatorı burada ekliyoruz.
    }
    $is_first_item = false; // İlk eleman artık işlendi.
    ?>

    <!-- Match item -->
    <li class="posts__item">
    <h6 class="posts__title">
    <?php echo esc_html($league_name . ':'); ?> <!-- League name -->
    </h6>
    <p class="posts__text">
    <?php echo esc_html($match_text); ?> <!-- Match details -->
    </p>
    </li>
    <?php endwhile; ?>
    </ul>
    </div><!-- .marquee -->
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    </div>
    </div>
    <!-- Matches Ticker / End -->
    • This topic was modified 1 year, 6 months ago by ozgurgedikli.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Savvas

    (@savvasha)

    Hi there @ozgurgedikli,

    As I can see from your code, you are using the following code to get your results:

    $main_results = sp_get_main_results( $event_id );

    $home_score = isset($main_results['home']['goals']) ? $main_results['home']['goals'] : null;
    $away_score = isset($main_results['away']['goals']) ? $main_results['away']['goals'] : null;$main_results = sp_get_main_results( $event_id );

    Are you sure that the $main_results array has the structure you are trying to use?

    Thanks,
    Savvas

    Thread Starter ozgurgedikli

    (@ozgurgedikli)

    Yes @savvasha ,

    That was during an experiment, I tried to capture the results like this, but it didn’t work.
    I tried this first but didn’t get any results

    thanks

    $event_id = get_the_ID();
    $home_score = get_post_meta( $event_id, 'sp_results_home', true );
    $away_score = get_post_meta( $event_id, 'sp_results_away', true );
    Plugin Contributor Savvas

    (@savvasha)

    Hi there @ozgurgedikli ,

    If you are using AI to help you on the code, have in mind that AI sometimes uses post_meta keys and functions that are not actually correct or real. For example, there is no sp_results_home or sp_results_awaypost_meta keys anywhere in SportsPress code.

    Try something like the following:

    $main_results = sp_get_main_results( $event_id );

    $home_score = isset($main_results[0]) ? $main_results[0] : null;
    $away_score = isset($main_results[1]) ? $main_results[1] : null;

    sp_get_main_results() function returns a simple array with just the values of your primary result (in your example “goals”)

    Thanks,
    Savvas

    Thread Starter ozgurgedikli

    (@ozgurgedikli)

    Thanks @savvasha

    With debugging I see that I can now pull the data in. However, I see that I am making errors during marquee printing.

    [10-Oct-2024 20:28:56 UTC] PHP Notice:  Undefined offset: 0 in /home/duzceama/public_html/wp-includes/meta.php on line 638
    [10-Oct-2024 20:28:57 UTC] Team 1: Takım2, Team 2: Takım1
    [10-Oct-2024 20:28:57 UTC] Main Results: Array
    (
    )

    [10-Oct-2024 20:28:57 UTC] Home Score: Bilinmiyor, Away Score: Bilinmiyor
    [10-Oct-2024 20:28:57 UTC] Match Time (UTC+3): 18:30
    [10-Oct-2024 20:28:57 UTC] League Name: 2.Amatör Lig (Play-Off)
    [10-Oct-2024 20:28:57 UTC] Post Status for Event ID 22210: publish
    [10-Oct-2024 20:28:57 UTC] Team 1: Takım1, Team 2: Takım2
    [10-Oct-2024 20:28:57 UTC] Main Results: Array
    (
    [0] => 12
    [1] => 8
    )

    [10-Oct-2024 20:28:57 UTC] Home Score: 12, Away Score: 8
    [10-Oct-2024 20:28:57 UTC] Match Time (UTC+3): 12:30
    [10-Oct-2024 20:28:57 UTC] League Name: 2.Amatör Lig (Play-Off)
    [10-Oct-2024 20:28:57 UTC] Post Status for Event ID 22209: publish
    [10-Oct-2024 20:28:57 UTC] PHP Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/duzceama/public_html/wp-includes/functions.php on line 5427
    Plugin Contributor Savvas

    (@savvasha)

    Hi there @ozgurgedikli ,

    From your error log I cannot say what exactly is the problem. In general, before getting the value of something inside an array, you should first check if it is set (with isset() function) like below:

    $home_score = isset($main_results[0]) ? $main_results[0] : null;

    Thanks,
    Savvas

    Thread Starter ozgurgedikli

    (@ozgurgedikli)

    Code errors were corrected, maquee printing was perfect. I completed the played matches, SCORES, and the matches to be played, by giving them color and style in the form of a program. I used Alchemists as Marguee general css data.

    I shared the codes to make it a nice share for Sportspress users.

    Marquee output ==> SKORLAR:10 Ekim 2024 => 2.Amatör Lig (Play-Off): Takım1 12 – 8 Takım2 | PROGRAM:11 Ekim 2024 => 2.Amatör Lig (Play-Off): Takım2 vs Takım1 – 18:30

    https://ibb.co/Tckt7ck

    Display with shortcode [matches_ticker title=”Matches” posts_per_page=”50″]
    <?php
    // Shortcode attributes
    $atts = vc_map_get_attributes($this->getShortcode(), $atts);
    extract($atts);

    // Set class for marquee wrapper
    $css_class = isset($css) ? 'marquee-wrapper ' . vc_shortcode_custom_css_class($css, ' ') : 'marquee-wrapper';
    $css_class .= isset($el_class) ? $this->getExtraClass($el_class) : '';

    // Query SportPress events (matches)
    $args = array(
    'post_type' => 'sp_event',
    'post_status' => array('publish', 'future'),
    'posts_per_page' => 10,
    'date_query' => array(
    array(
    'after' => date('Y-m-d', strtotime('-4 days')), // 4 gün önce
    'before' => date('Y-m-d', strtotime('+4 days')), // 4 gün sonrası
    'inclusive' => true,
    'type' => 'DATE',
    ),
    ),
    );

    $events_query = new WP_Query($args);

    $completed_matches = [];
    $upcoming_matches = [];
    ?>

    <!-- Matches Ticker -->
    <div class="<?php echo esc_attr($css_class); ?>">
    <div class="container">

    <?php if (!empty($title)) : ?>
    <div class="marquee-label">
    <?php echo esc_html($title); ?>
    </div>
    <?php endif; ?>

    <div class="marquee" data-gap="10" data-duplicated="false">
    <ul class="posts posts--inline">
    <?php if ($events_query->have_posts()) : ?>
    <?php
    while ($events_query->have_posts()) : $events_query->the_post();
    $event_id = get_the_ID();

    // Takımların ID'lerini al
    $teams = sp_get_teams($event_id);

    if (is_array($teams) && count($teams) > 1) {
    $team1_id = $teams[0];
    $team2_id = $teams[1];

    $team1_name = get_the_title($team1_id);
    $team2_name = get_the_title($team2_id);
    } else {
    // Takımlar eksikse "Bilinmiyor" olarak ayarlıyoruz
    $team1_name = $team2_name = 'Bilinmiyor';
    }

    // Maç skoru alalım
    $main_results = sp_get_main_results( $event_id );

    if (!empty($main_results) && count($main_results) >= 2) {
    $home_score = $main_results[0];
    $away_score = $main_results[1];
    } else {
    $home_score = 'Bilinmiyor';
    $away_score = 'Bilinmiyor';
    }

    // UTC+3 saat dilimine göre maç saati
    $match_time_utc = get_post_time('H:i', true, $event_id);
    $match_time_utc3 = strtotime($match_time_utc) + (3 * 3600); // UTC+3
    $formatted_time = !empty($match_time_utc) ? date('H:i', $match_time_utc3) : 'Bilinmiyor';

    // Lig adı
    $leagues = wp_get_post_terms($event_id, 'sp_league', array('fields' => 'names'));
    $league_name = !empty($leagues) ? $leagues[0] : 'Lig Bilinmiyor';

    // Maç tarihi
    $date_html = get_post_time('Y-m-d', false, $event_id);
    $formatted_date = !empty($date_html) ? date_i18n('j F Y', strtotime($date_html)) : 'Bilinmiyor';

    // Maçın durumu: Yayınlanmış mı?
    $post_status = get_post_status($event_id);
    $current_time = current_time('Y-m-d H:i');

    if ($post_status == 'publish' && !empty($home_score) && !empty($away_score) && $date_html < date('Y-m-d')) {
    // Completed matches
    $completed_matches[] = '<span style="color:#3904dd;">' . esc_html($formatted_date . ' => ' . $league_name . ': ' . $team1_name . ' ' . $home_score . ' - ' . $away_score . ' ' . $team2_name) . '</span>';
    } elseif ($post_status == 'future' || ($date_html >= date('Y-m-d') && $match_time_utc3 > strtotime($current_time))) {
    // Upcoming matches
    $upcoming_matches[] = '<span style="color:#ff0404;">' . esc_html($formatted_date . ' => ' . $league_name . ': ' . $team1_name . ' vs ' . $team2_name . ' - ' . $formatted_time) . '</span>';
    } else {
    $upcoming_matches[] = '<span style="color:#ff0404;">' . esc_html($formatted_date . ' => ' . $league_name . ': ' . $team1_name . ' vs ' . $team2_name . ' Bilinmiyor') . '</span>';
    }
    endwhile;
    ?>

    <!-- Display completed matches -->
    <?php if (!empty($completed_matches)) : ?>
    <li class="posts__item">
    <h6 class="posts__title">SKORLAR:</h6>
    <p class="posts__text">
    <?php echo implode(' | ', $completed_matches); ?>
    </p>
    </li>
    <?php endif; ?>

    <!-- Display upcoming matches -->
    <?php if (!empty($upcoming_matches)) : ?>
    <li class="posts__item">
    <h6 class="posts__title">PROGRAM:</h6>
    <p class="posts__text">
    <?php echo implode(' | ', $upcoming_matches); ?>
    </p>
    </li>
    <?php endif; ?>

    <?php else : ?>
    <!-- If no matches are available, show this message inside the marquee -->
    <li class="posts__item">
    <p class="posts__text">HAFTALIK MÜSABAKALAR YAYINLANMADI</p>
    </li>
    <?php endif; ?>
    </ul>
    </div><!-- .marquee -->
    <?php wp_reset_postdata(); ?>

    </div>
    </div>
    <!-- Matches Ticker / End -->
    • This reply was modified 1 year, 6 months ago by ozgurgedikli.
    Plugin Contributor Savvas

    (@savvasha)

    Great work @ozgurgedikli and thanks for sharing your final code !!!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Sportspress match scores pull’ is closed to new replies.