• Resolved nimeck

    (@nimeck)


    I tried to follow your lead with Chat GPT to make a shortcode that lists all the matches by team. However it outputted something very different.

    Then I tried to alter the Venues extension code from the other post, however it did not work. Could you tell me if there is something I screwed up here?

    Shortcode I entered was: [team_matches team_id=”14″]

    <?php
    /*
    Plugin Name: Football Pool - Matches by Team Shortcode
    Description: This plugin adds a shortcode to display matches for a certain team.
    */
    
    // Main plugin class
    class Team_Matches_Shortcode {
        
        public function __construct() {
            // Hook to plugins_loaded to ensure dependencies are loaded
            add_action('plugins_loaded', array($this, 'init'));
        }
        
        // Initialize the plugin
        public function init() {
            // Add shortcode
            add_shortcode('team_matches', array($this, 'display_team_matches'));
        }
        
        // Shortcode function
        public function display_stadium_matches($atts) {
            // Instantiate Football Pool classes
            $pool = new Football_Pool_Pool;
            $team = new Football_Pool_Team;
    
            // Get team ID from shortcode attribute
            $team_id = isset($atts['team_id']) ? intval($atts['team_id']) : 1;
    
            // Get stadium details
            $team = $team->get_stadium_by_id($team_id);
    
            // Get matches for the stadium
            $fixtures = $team->get_plays();
    
            // Return the matches using Football Pool's print_matches method
            return $pool->matches->print_matches($fixtures, 'page team-page');
        }
    }
    
    // Instantiate the plugin class
    new Team_Matches_Shortcode();
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author AntoineH

    (@antoineh)

    The main thing that goes wrong here, is that you overwrite the $teams variable by using a method for it that doesn’t exist. Or, in other words, the parts related to stadiums were not removed from the code or replaced by bits that you need for teams.

    I think that the code can be really simple. You can initiate a Football_Pool_Team object directly by passing the team ID. And then use the get_plays() method to get the matches. E.g. something like this (not tested, though):

        // Shortcode function
        public function display_team_matches($atts) {
            // Get team ID from shortcode attribute
            $team_id = isset($atts['team_id']) ? intval($atts['team_id']) : 1;
    
            // Instantiate Football Pool classes
            $pool = new Football_Pool_Pool;
            $team = new Football_Pool_Team($team_id);
    
            // Get matches for the stadium
            $fixtures = $team->get_plays();
    
            // Return the matches using Football Pool's print_matches method
            return $pool->matches->print_matches($fixtures, 'page team-page');
        }

    You may want to add some extra checks in the code for cases where you supply an invalid or non-existing team ID, but since you are the only one using this, you can also live without it (it will just throw an error and you can fix it by changing the ID in the content).

    Thread Starter nimeck

    (@nimeck)

    Thank you! This helped. Appreciate the suggestion.

    For anyone looking to do the same, you can download the extension here.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List Matches by Team Shortcode (extension)’ is closed to new replies.