I am having issues with my code. I am pretty certain that it is related to quotations. Let me demonstrate.
This works fine:
<?php if ( $is_latest_post ) echo '
hello world
'; ?>
This doesn’t work:
<?php if ( $is_latest_post ) echo '
<a class="recent<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>" href="<?php bloginfo('url'); ?>/<?php echo $category[0]->category_nicename; ?>"><?php echo $category[0]->cat_name; ?></a>
'; ?>
Why doesn’t it work? What can I change to make it function? The a class part of the code works perfectly outside the if ( $is_latest_post ) statement. Appreciate that I have only just started learning coding with PHP.
when you encapsulate strings in single quotes, it treats that string as a literal. PHP will not parse the code you have in that string.
What you need is concatenation. The dot operator
.lets you mash several strings together.You can concatenate arbitrarily many things together.