Dette indlæg indeholder funktioner der er rare at kende, når du bygger dit eget theme. Det er nok et indlæg der kommer til at vokse med tiden...
Det berømte loop
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
$i = 0;
while ($the_query->have_posts()){
$the_query->the_post();
?>
<div class="col s12 m4">
<div class="card">
<div class="card-image">
<img src="<?php echo catch_that_image(); ?>" alt="" title="<?php the_title(); ?> (I kategorien <?php echo get_the_category( $post->ID )[0]->cat_name; ?>)">
<span class="card-title"><?php the_title(); ?></span>
</div>
<div class="card-content">
<p><?php echo wp_trim_words(get_the_content(),18); ?></p>
</div>
<div class="card-action">
<a class="blue-text" href="<?php echo the_permalink(); ?>"><?php the_title(); ?></a>
<?php echo '<a href="'.get_home_url().'/'.get_the_category($the_query->post->ID)[0]->taxonomy.'/'.get_the_category($the_query->post->ID)[0]->slug.'">'.get_the_category($the_query->post->ID)[0]->name.'</a>
'; ?>
</div>
</div>
</div>
<?php
$i++;
if($i%3===0){
echo '<div class="row"> </div>';
}
}

<pre>
/////////////////// functions.php
# Brug et billede i din post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
/////////////////// i f.eks. front-page.php
...
<img src="'.catch_that_image().'" ...
##############################################
// udskriv teksten til dit billede
// functions.php
function catch_that_image_figcaption() {
global $post, $posts;
$first_img_figcaption = '';
ob_start();
ob_end_clean();
$output = preg_match_all('#<\s*?figcaption\b[^>]*>(.*?)</figcaption\b[^>]*>#s', $post->post_content, $matches);
$first_img_figcaption = $matches[1][0];
if(empty($first_img_figcaption)) {
$first_img_figcaption = 'ingen billed-tekst';
}
return $first_img_figcaption;
}
...
I f.eksk. front-page.php
<?php echo catch_that_image_figcaption(); ?>
</pre>