Vi har två sätt att modifiera innehåll via hooks
add_action('init', 'my_custom_function');
add_filter('the_title', 'my_title_function');
https://codex.wordpress.org/Plugin_API/Action_Reference
function profanity_filter($text) {
$words = array("illuminati", "big brother", "deep state");
/* str_replace checks for words in the array and removes them from
* the content in the post, returning a bleeped version */
return str_replace($words, "BLEEP", $text);
}
add_filter('the_content', 'profanity_filter');
Template
<?php
/**
* Template Name: This Name Matters
*/
Spelar mindre roll vad filen heter, ha rätt Template Name
Vi ska aldrig gå direkt till en Template, sätt den alltid via Post.
Den titel du ger blir the_title()
Länken får du via the_permalink()
Den bild som välj här blir the_post_thumbnail()
Det innehåll som du lägger här blir the_content()
Vill du lägga på en viss klass på bilden
the_post_thumbnail(
array(
"class" => "custom-image-class"
)
)
Många funktioner tar en array av argument
Visa bara bilder, t.ex. portfolio
Sätt en kategori och en "Featured Image"
$query = new WP_Query(array("category_name" => "portfolio"));
while($query->have_posts()): $query->the_post();
the_post_thumbnail();
endwhile;
Gör det till en template
Plugins lägger till extra funktionalitet
Plugins är i sin grund en php-fil som du aktiverar eller avaktiverar
All kod som du skrivit i functions.php kan du lägga i en plugin
<?php
/**
* Plugin Name: Custom Plugin Name
*/
Allt som krävs är en kommentar i börja av filen för att definera den som plugin
Samt att lägga den i wp-content/plugins
/**
* Plugin Name: Profanity filter
*/
function profanity_filter($text) {
$words = array("illuminati", "big brother", "deep state");
/* str_replace checks for words in the array and removes them from
* the content in the post, returning a bleeped version */
return str_replace($words, "BLEEP", $text);
}
add_filter('the_content', 'profanity_filter');
Custom Post Type
Default Post Types
function create_post_type() {
register_post_type( 'Cats',
array(
'labels' => array(
'name' => 'Cats',
'singular_name' => 'Cat'
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'create_post_type' );
$args = array( 'post_type' => 'cats' );
$query = new WP_Query( $args );
Filtrera baserat på vilken typ av Post vi har
https://codex.wordpress.org/Post_Types
https://www.smashingmagazine.com/2012/11/complete-guide-custom-post-types/
Plugin Name
post_type