WP developer,
I am using the below code to display h3 heading and post tags as title and it worked fine, however i eliminate it to work only on specific pages, it is now sitewide and used everywhere.
Example is here:
https://readnational.com/national-birds/
<h3 class="entry-title">
<a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>">
<?php
$posttags = get_the_tags();
if ($posttags) {
echo '<ul class="symbolic">';
echo '<li>' . $posttags[1]->name . '</li>';
echo '</ul>';
}
?>
</a>
</h3>
Now, i want to apply wordpres conditonal tag to run the code only for specific pages, I did it with conditaional tags using page IDs but its not working
<?php if(is_page([1364,211])){
<h3 class="entry-title">
<a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>">
<?php
$posttags = get_the_tags();
if ($posttags) {
echo '<ul class="symbolic">';
echo '<li>' . $posttags[1]->name . '</li>';
echo '</ul>';
}
?>
</a> } <?php endif; ?>
</h3>
I tried it a lot to use the code for specific pages using the conditional tags by Page IDs, but it is not working, also it is giving syntax error, So how to do it, kindly share full code properly,
2
Answers
The array is working good to add as many pages id as we want, Thanks to @Chris Haas
PHP has what’s called "Alternative syntax for control structures" and although a lot of people don’t like it, I think it is really great for developers that might not be as comfortable in PHP as they are in HTML. It allows you you mentally separate your PHP and HTML in a clearer way, at least IMHO.
Rewriting your code with that in mind, and also introducing a little more escaping as well as a loop over the tags gives you this:
That loop I introduced might not be what you want, however, and you might literally want the second item from the tag array always. If that’s the case, this version might be what you want.