I'm new to PHP coding and am trying to make this section output post from a specific category, say "Events". I've tried looking at different examples but couldn't find anything that seemed to fit the coding in this PHP template. I would think it is a simple addition that I am missing.
Here is the current code I have:
<?php $args=array( 'posts_per_page'=> 3, 'post_type' => 'post'); $myposts = get_posts( $args ); foreach ($myposts as $post) { ?>
<?php $content=$post->post_content; ?>
<?php $contnt=substr($content, 0, 150);?>
<?php if(has_post_thumbnail()){ ?>
<p>
<div style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
<?php the_post_thumbnail(); ?>
</div>
</p>
<?php } else { ?>
<img src="<?php echo site_url();?>/wp-content/uploads/2014/10/noimageavailable2.jpg" style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
<?php } ?>
<h4> <a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h4>
<p>
<?php the_excerpt();?>
.....</p>
<hr class="line-dots clearbreak">
<?php } ?>
Any insight or direction would be greatly appreciated.
Thanks!
First I recommend you to appart your logic from your template. This will help you to understand better your code.
Now, php has a template syntax. I could translate your code like this:
<?php
$args=array( 'posts_per_page'=> 3, 'post_type' => 'post');
$myposts = get_posts( $args );
foreach ($myposts as $post): ?>
<?php if(has_post_thumbnail()): ?>
<p>
<div style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
<?php the_post_thumbnail(); ?>
</div>
</p>
<?php else: ?>
<img src="<?php echo site_url();?>/wp-content/uploads/2014/10/noimageavailable2.jpg" style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
<?php endif; ?>
<h4><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h4>
<p><?php the_excerpt();?></p>
<hr class="line-dots clearbreak">
<?php endif; ?>
<?php endforeach; ?>
Now it looks much easier to understand, doesn't it?