Ein tolles Feature für Online-Spiele-Seiten ist, wenn auf der Startseite immer zufällige Spiele aus verschiedenen Kategorien angezeigt werden. Dies hat den Vorteil, dass auch ältere Spiele gesehen und gespielt werden.
Normaler WordPress Blog zeigt auf der Startseite nur die neuesten Beiträge/Spiele an.
Das Beispiel mit den zufällig angezeigten Spielen kann man bei FunGames24.net in Action sehen.
Im heutigen Tutorial werde ich Schritt für Schritt zeigen, wie einfach so was mit WordPress realisiert werden kann. Dazu muss nur die „index.php“ des jeweiligen Themes angepasst werden.
Grundlagen
Bevor wir mit der Implementierung anfangen, solltest Du Dir auf jeden Fall noch die Dokumentationen von folgenden WordPress-Funktionen anschauen:
Vorüberlegung
Vor der Realisierung sollte man sich überlegen, wie so etwas realisiert werden kann. Für die Anzeige von zufälligen Spielen sah meine Überlegung in etwa so aus:
- Lese alle Kategorien aus
- Gehe die Kategorien durch und wähle aus jeder Kategorie 6 zufällige Spiele aus
- Gehe alle ausgewählten Spiele einer Kategorie durch und hole für jedes Spiel folgende Daten:
- Name des Spiels
- Permalink des Spiels
- Vorschaubild
Bei Spielnamen muss man beachten, dass manche Spiele sehr lange Namen haben und oft das Theme „zerschießen“. Deswegen sollte der Name eines Spiels auf gewünschte Länge gekürzt werden. Dazu habe ich folgende PHP-Funktionen verwendet:
- strlen – um die Länge eines Spielnamens zu ermitteln
- substr – um den Namen an einer bestimmten Stelle abzuschneiden
Realisierung
Da wir und schon im Vorfeld überlegt haben, wie Zufalls-Spiele angezeigt werden sollen, ist die Realisierung ein Kinderspiel. Wie vorhin erwähnt, muss für unser Vorhaben nur die „index.php“ des jeweiligen Themes angepasst werden. Die Datei einfach mit einem Texteditor (Notepad++, Programmer’s Notepad..) öffnen und bearbeiten:
Hier der Beispiel Code:
cat_ID;
// Get XX random games from this category
$games = get_posts('numberposts='.$random_games.'&orderby=rand&category='.$cat_id);
// Check if this category is empty?
if ($games) {
// This category has games..
// Beginn - Category block
echo '';
// Show the category name in the block header
echo '' .$category->name. '
';
// Go through each game and display all relevant informations
foreach ($games as $post) {
?>
More Games';
// END - Category block
echo '
Hi Daniel,
Can u translate it to english? =x
Thanks alot
Hi weizai,
at the right site you can find „Translate to your language“. Pick there your language flag to translate the post with google. It’s not perfect translation, but it should be enough to understand the tutorial.
The code comments are already in English 🙂
Let me know, if you need help..
Greets
Daniel
Hi Daniel,
I am using your CSS gallery theme. Which part of index.php should i change?
Tried a few times but still cant get it done. 🙁
Thanks in advance.
Regards
This is a example code. If you wan’t to use it with CSS Gallery you have to edit the index.php between <div id=“content“> and </div><!– End CONTENT Div –>
ohh..I thought copy the code to and paste it to replace in the index.php. =x
that means I have to edit your this code?
I don’t think I am able to do it, weak in php =x
Regards
Hi Daniel,
I am able to put it to the code already!
But I have some problems.
1) At the CSS gallery’s css.style sheet, which part do I need to delete so that I can use this css.style sheet. Because if I just use this, it will overwrite and the page will be jumbled up.
2) I changed the $random_games = 6; to $random_games = 4;
but the page still appear 6 games. No idea why. 🙁
Thanks in advance!
Regards
Hi weizei,
I’ve made a mistake in my tutorial.
replace numberposts=6 with numberposts=$random_games
At the moment I’ve some trouble with my site. When I fix website problems I will update this tutorial…
Okay Daniel!
Thanks for the help 😀
Thanks 😉
Hi,
I want to make my own custom box, say most viewed, top rated etc how would I do so?
Regards.
Hello, I did everything like you said but I have problems with css.
I put php code in index php (didnt deleted anything) and added css lines.
Maybe you could upload sample files?
Hi Cocetz,
OK, I will do that 🙂
Hi Perry,
you need this two plugins:
1. WP-Postrating
2. WP-Postviews
Greets
Yes. But how do I display them?
Show Highes Rated:
<?php if (function_exists('get_highest_rated')): ?>
<ul>
<?php get_highest_rated(); ?>
</ul>
<?php endif; ?>
Show Most Viewed
<?php if (function_exists('get_most_viewed')): ?>
<ul>
<?php get_most_viewed(); ?>
</ul>
<?php endif; ?>
Hi.
How would I display them like the categories on the index page?
Regards
Hi Daniel,
Sorry to trouble you again.
I got some problems ( The displayed games in the home page does not align properly) in viewing my site with Internet Explorer after implementing this CSS style. But when I use Firefox it works fine.
Any idea?
Thanks in advance
Hi wäre sehr nett wenn du uns sagen würdest wie man das ins css gallery einbaut kenn mich leider mit php gar nicht aus danke.