Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 7022
Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /var/www/jonasnordstrom.se/wp/wp-includes/functions.php on line 2162
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 2147483648 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-content/plugins/wordfence/lib/wordfenceClass.php on line 1116
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Implicit conversion from float 4294967295 to int loses precision in /var/www/jonasnordstrom.se/wp/wp-includes/pomo/mo.php on line 208
Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Cookie/Jar.php on line 63
Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Cookie/Jar.php on line 73
Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Cookie/Jar.php on line 89
Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Cookie/Jar.php on line 102
Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Cookie/Jar.php on line 111
Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40
Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51
Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68
Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82
Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/jonasnordstrom.se/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91 januari 2010 – Jonas Nordström
I WordPress 2.5 introducerades shortcodes men det är nog många som fortfarande inte vet riktigt vad det är och vad man kan använda det till.
Shortcodes tillåter dig exempelvis att skriva [gallery] direkt i ett blogginlägg, och så visas alla bilder som hör till det inlägget som en liten kontaktkarta.
Man kan givetvis definiera egna shortcodes, genom att skriva en funktion i ett plugin eller i functions.php och sen koppla en shortcode till den funktionen.
Funktionen add_shortcode registrerar en ny shortcode, och i exemplet ovan betyder det att om man skriver [bloginfo] i ett inlägg, så kommer funktionen bloginfo_shortcode() att anropas. Just i det här exemplet ska man dessutom skicka med en parameter, det ser ut så här: [bloginfo key=”template_url”]
(Exemplet kommer från Blue Anvil)
Exempel 2
Jag har i ett tidigare inlägg visat hur man kan koppla en spotifylista till en wordpress-post, en metod som jag använder på http://topp30.se.
Jag vill utnyttja det faktum att listorna nu ligger som egna fält, och skapa en sida med alla spotifylistor i en lång (nåja) HTML-lista.
Steg ett är att definiera en funktion som hämtar alla inlägg med ”spotify-fält” och sen generera en lista av dem.
Nästa steg är att koppla en shortcode till den funktionen.
// [spotifylist] - shortcode function
function spotify_sc_func($atts) {
ob_start();
?>
posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'spotify'
AND wposts.post_status = 'publish'
AND wposts.post_date < NOW()
ORDER BY wposts.post_type, wposts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
På sajten http://topp30.se listar jag och en del vänner och bekanta varje år våra favoritlåtar. Vi har sysslat med detta något nördiga listande i drygt tio år. Förra året började några av oss att bifoga listan i spotify-form, och inte bara text. I år har nästan alla 20 en spotifylista för sin årslista.
Länken till spotifylistan finns i varje inlägg som en vanlig textlänk, men den ligger ofta på olika ställen, har olika ankartext och kan vara lite svår att hitta. Därför la jag till en genererad spotifylänk som alltid hamnar på samma ställe, uppe till höger i varje inlägg. Lättare att hitta, mer konsekvent.
Detta kan man åstadkomma genom att lägga spotifylänken som ett ”eget fält”, ändra i varje template-fil på rätt ställe (index.php/archive.php/search.php etc.) och där läsa in spotify-fältet och visa länken, men det blir opraktiskt att underhålla, lätt att göra fel och lite fult.
Steg 3:
Lägg till ett filter, som anropar spotify_content varje gång WordPress-funktionen the_content anropas. Det är the_content som visar texten i ett blogginlägg så den kommer definitivt att anropas från exempelvis single.php och index.php (det finns undantag, se längre ner)
add_filter( 'the_content', 'spotify_content' );
Steg 4:
Lägg till en css-klass i style.css som lägger din div som du vill ha den. Den allra enklaste varianten blir så här:
.spotify-link {
float: right;
}
Och det var allt. Resultatet, med en klickbar spotify-logga för varje spotifierat inlägg blir så här: topp30.se med automatisk spotifylänk
Observera att ibland används the_excerpt stället för the_content i mallarna. Det är när man bara vill visa ett utdrag ur inläggen istället för hela texten.
Om man vill ha spotify-länken även i dessa mallar så är det inte svårare än att man lägger in ett filter för the_excerpt också: