skip to Main Content

In my WordPress website i made this code in header.php that write html under the header only if you are on a specific post (product) page,

<?php
    $post = get_post();
        if ( $post->ID == 'postid' ){
            echo '<div>
                <p>'text'
                </p>
            </div>
        </div>
    </div>';
}
?>

I would like to make the same thing in a specific category page doing something like this

<?php
$postcat = get_the_category();
    if ( $postcat->ID == 'categoryid'){{
        echo '<div>
            <p>'text'
            </p>
        </div>
    </div>
</div>';
}
?>

But it doesn’t work, i also tried different method like

<?php if (is_category('categoryname')) : ?>
   <div></div>
?php endif;?>

How can i do this?

Thanks.

2

Answers


  1. You can use is_product_category. check the below code.

    <?php
        if ( is_product_category( 'your-category-slug' ) ){
                    echo '<div>
                        <p>'text'</p>
                    </div>
                </div>
            </div>';
        }
    ?>
    

    USEFUL LINKE

    Login or Signup to reply.
  2. If post = specific ID

    <?php
    $post = get_post();
    if ( $post->ID === 'postid' ){
        echo "<div>";
        echo $post->post_content;
        echo "</div>";
    }
    ?>
    

    Is post has category "supercat"

    <?php
    $post = get_post();
    if ( has_category('supercat', $post->ID)){
        echo "<div>";
        echo $post->post_content;
        echo "</div>";
    }
    ?>
    

    For products , check Bhautik’s answer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search