This is what ‘Training times’ section looks like static –
Next step is to turn this static section into dynamic with WordPress.
First of all, this section will be created with Pods therefore decision on what the structure of Pods is going to be like, needs to be made.
First thought about the structure of Pod
Create a Pod called ‘training times’ and then in there create fields such as start time, end time and session.
Then go to actual pod and create Monday, Tuesday etc. and assign start time, end time and session to days of the week.
However, this initial thought about the structure of Pod has a problem as there are more times and sessions in one day (most of them are 3 different sessions). Therefore this is not going to work. Another potential idea will be to create more start – end time and session (all together 3 for each day).
After further thinking, conclusion was made that this is also not a good way of solving the structure out as no one can guarantee that the club is going to have maximum of three sessions per day, i.e. the club may decide to run a 4th session.
Next solution would be to create two Pods with a relationship (relational databases).
Creating Pods with a relationship (relational databases)
There are relationships like
- one to one – one order may have one invoice
- one to many – one person may have many orders
- many to many – the same session running on multiple weekdays
This section needs to have one to many relationship as one day needs to have many sessions. Even on the days were there is one session, it will still be a one to many because it has the possibility to have many sessions.
Next step is to create two Pods as one weekday will have many sessions –
In ‘Weekdays’ pod create field called ‘Weekdays’ and in ‘Sessions’ pod create field called ‘Weekdays’ too. Then link them through relationship.
By linking them through relationship, one to many relation has been created. i.e. one weekday has many sessions.
The post title will be used for session description to tell users what type of session is available.
However, a few sessions run more than in once a week, i.e. kadets (5-9) are on Monday and Thursday. This is change that needs to be made in Pods Admin area.
Go into the ‘Additional Field Options’ tab of the ‘Weekdays’ field in ‘Sessions’ pod. Change ‘Single select’ to ‘Multiple select’. Also Format of the field can be changed but this time is Checkboxes right.
By setting ‘Multiple Select option’ there is no longer a one to many relationship, there is many to many as the same session can run on multiple weekdays.
Now that it is all set up, days of the week need to be created. Go to Pod ‘Weekdays’ and create Monday, Tuesday, Wednesday etc. At the moment all sessions of the club run on all days of the week apart from Friday and Sunday but these two days will be still created as the club may decide in the future to run sessions on them days.
This is what it looks like after opening one of the days –
Session tickbox says ‘Weekdays’, it should say ‘sessions’, but this is a small change that can be done any time by editing the label in the pods admin.
Now, go to ‘Sessions’ pod and create all sessions that are run by the club.
However, there are a two sessions that are run on different days in different time therefore, decision to create session with the same name just assign it to different day and time has been made. This is because training times are going to be loop through each weekday. Then, from each weekday, loop through the sessions that belong to that weekday. This means that the right session is going to be pulled in by certain day. Also weekdays are not going to be in alphabetical order therefore this will need sorting out later on.
After creating sessions, all the work in back end is done, now it is time to add code in order to display the content.
Creating loop to display the content
This is the code that needs to be add to the code in ‘page-services.php’ –
Copy and paste the code into own code and change ‘pod_name’ to ‘weekday’, ‘relationship_field’ to ‘sessions’ and ‘some_field’ to ‘weekday’ in order to match names of fields and pods with the ones that has been created in Pods Admin.
Save and check the result on the website. However, it is not displaying anything on the website therefore there must be some kind of error. Also noticing that actual days of the week like Thursday – at the bottom, there should be 3 sessions ticked as that is what it is set up in Sessions but there is only one.
Further investigation about why this code is not working and why there are not more sessions ticked in certain weekdays will be done.
Investigation of occurred problems
First of all, lets sort out why there are not more sessions ticked in certain weekdays.
This may be happening because ‘multiple select’ on both sides of the relationship (on weekday and sessions) has not been selected. However, after check, this is not the case as multiple select is on both sides of the relationship.
Next question is ‘What happens after ticking multiple sessions and save, do they just untick after it is saved?’
After testing that, ticked sessions stayed ticked after saving so this suggest that it is working as it should be so this problem is solved.
Next step is to find out why the code is not working.
Go back to code on https://gist.github.com/Shelob9/7832561#file-2-related-fields-example-php and copy it into the file ‘page-services.php’ like it was done earlier.
The ‘get_the_id()’ is only there if this is used on a specific page, so delete that and replace it with
$params = array(); For now, params will be empty, this should go above $pod = pods( ‘weekday’, $params ); . Pod_name will be replaced with ‘weekday’ as this is name of the pod. ‘relationship_field’ will be replaced with ‘sessions’ and ‘some_field’ with ‘start_time’. Also copy the full line and create new one called ‘end_time’.
Save the code and check the result.
Unfortunately, it is not outputting anything.
Go to https://gist.github.com/Shelob9/7832561#file-1-pods-loop-general-example-php and copy line 9-12 from the code and place it above ‘$related =
$pod->field( ‘relationship_field’ );’ –
Also the copied code needs ‘s’ removing from variable ‘&pods’ to ‘&pod’ in order to match already existing variable in the code.
Another step is to add curly brackets at the end of the full code because if statement and a while statement was opened when the lines 9-12 were copied and pasted into the code and the statements need to be closed otherwise it will error. Simply add two closing curly braces for both the while and if under the code within the while loop, so it will look like –
Save the code and check the result.
Unfortunately, it is still outputting nothing.
Next step is to check if relationships are working. To do that ‘echo
$pod->total();’ bellow ‘$pod = pods( ‘weekday’, $params );’ to see if it is outputting any content.
After checking on the website, there was number 7 outputted. However, possibility of having white text showing on white background were there and this could be the reason why there is only number 7 displayed, therefore the viewing source code was necessary to find out if that was the case. Source code contained only number 7 so this was not the case.
This suggested that the relationships may not be working therefore adding ‘var_dump($related);’ under ‘$related = $pod->field( ‘sessions’ );’ This is useful function to check if relationships are working right.
Result showed that there is an empty array which suggest that something is broken on the weekday relationship to the session.
Next step is to try and edit one of the weekdays posts in Weekdays pod – for example Monday has been changed to Mond. Go to ‘Sessions’ pod and check if anything changed in field ‘Weekdays’.
It has changed correctly so it looks all fine.
Go to ‘Weekdays’ pod and click on any post to check fields –
Here is the problem as one of the field has been named incorrectly. Click ‘Pods admin’ on the bottom left, then find ‘Weekdays’. There is a field called ‘weekdays’ which should be called ‘sessions’. Edit that.
Make outputted data look like they suppose to –