You’re not adding HTML code to your file, you’re adding invalid PHP.
To recap, in PHP everything between <?php
and ?>
is PHP code.
So this is valid PHP:
<?php echo 'hello world';! ?>
This is also valid:
<b><?php echo 'hello world'; ?></b>
This is not valid:
<?php <b> echo 'hello world'; </b> ?>
Why? Because is invalid PHP. It’s a valid HTML tag yes, but you didn’t put it in HTML, you put it in-between the tags so it’s PHP code, and it isn’t valid PHP code.
Sidenotes
- You’re not indenting your code correctly. Any good editor will auto-magically do it for you, PHPStorm has a reformat option, Sublime auto-indents as you type, save yourself time and effort and use an editor that does it for you. Indenting makes your code easier to read, and bugs easier to spot
- Use brackets instead of shorthand, e.g.
if( this ) { do that }
, it has wider support among editors and checkers. It’s too easy to add strayendwhile;
statements ( your original code has 1 while loop, but you close it twice, this makes no sense and only serves to confuse ). If you use{
and}
a good editor will automatically type the closing bracket for you and indent it accordingly.
So in conclusion, your code should look more like this:
<?php
if ( have_posts() ) {
echo '<ul>';
while ( have_posts() ) {
the_post();
echo '<li>';
get_template_part( 'contentArchive', get_post_format() );
echo '</li>';
}
echo '</ul>';
}
Changes:
- Used echo to print out the html
- removed extraneous blank lines and converted to tabs
- fixed indenting
- Used braces instead of PHP shorthand syntax
- removed the trailing PHP closing tag, closing tags are unnecessary at the end of a PHP file if the last thing is PHP code