Home > OS >  How to Use If-Else Condition to Display the Result
How to Use If-Else Condition to Display the Result

Time:09-01

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 >                
    <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 >';
                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 >                
    <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 >';
                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,

CodePudding user response:

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:

<?php if (is_page([1364, 211])): ?>
    <h3 >
        <a href="<?php echo esc_url(get_permalink()); ?>" title="<?php esc_attr_e(get_the_title()); ?>">
            <?php if ($posttags = get_the_tags()) : ?>
                <ul >
                    <?php foreach ($posttags as $posttag): ?>
                        <li><?php esc_html_e($posttag->name); ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </a>
    </h3>
<?php endif; ?>

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.

<?php if (is_page([1364, 211])): ?>
    <h3 >
        <a href="<?php echo esc_url(get_permalink()); ?>" title="<?php esc_attr_e(get_the_title()); ?>">
            <?php if ($posttags = get_the_tags()) : ?>
                <ul >
                    <li><?php esc_html_e($posttags[1]->name); ?></li>
                </ul>
            <?php endif; ?>
        </a>
    </h3>
<?php endif; ?>

CodePudding user response:

<?php if (is_page( array(1364, 2011) ) ) : ?>
    <h3 >
        <a href="<?php echo esc_url(get_permalink()); ?>" title="<?php esc_attr_e(get_the_title()); ?>">
            <?php if ($posttags = get_the_tags()) : ?>
                <ul >
                    <li><?php esc_html_e($posttags[1]->name); ?></li>
                </ul>
            <?php endif; ?>
        </a>
    </h3>
<?php endif; ?>

The array is working good to add as many pages id as we want, Thanks to @Chris Haas

  • Related