mercredi 9 juin 2021

wordpress if condition the correct way

Im trying to find the correct way in WP to display different content based on different conditions.

For example if im in a certain category then show text Category, if year archive then year, if Tag then tag and so on.

I can do this by checking against the URL, seeing if "archive", "tag" or "category" is in the URL, but I would like to do it via the type of post showing in case of a future URL update.

I currently have tried

 <?php
    $current_tag = single_tag_title("", false);
    $archive_year = get_the_time('Y');
    if (is_archive()) {
        ?>
        <div class="row">
            <div class="col-md-8">
                <h1 class="pt-2"><?php echo 'Year ', $archive_year ?></h1>
            </div>
        </div>
        <?php
    } elseif (is_category) {
        ?>
        <div class="row">
            <div class="col-md-8">
                <h1><?php echo single_cat_title('', false); ?></h1>
            </div>
        </div>
        <?php
    } elseif (is_tag()) {
        ?>
        <div class="row">
            <div class="col-md-8">
                <h1>tag working <?php echo the_title(); ?></h1>
                <p>Tag: <?php echo single_tag_title(); ?></p>
            </div>
        </div>
        <?php
    } else {
        ?>
        <div class="row">
            <div class="col-md-8">
                <h1><?php the_title(); ?></h1>
            </div>
        </div>
        <?php
    }
    ?>

yet I feel this is not right, any help on this matter would be mutch apriciated

Thanks

Aucun commentaire:

Enregistrer un commentaire