WordPress.org

Ready to get started?Download WordPress

Forums

IF ELSE code for body ID check (3 posts)

  1. natpoit7
    Member
    Posted 1 year ago #

    Hi,

    I need to use different ID style for the body on some pages, and one different ID for all other pages. I have a code

    <?php if (is_front_page()) {
    ?>
    <body>
    
    <?php
    }
    elseif (is_page( 'page1'||'page2'||'page3'||'page4' ) ) {
    ?>
    <body id="<?php echo get_query_var('name'); ?>">
    
    <?php } else { ?>
    <body id="defaultid">
    
    <?php } ?>

    It works for page1, page2, page3, page4: I
    <body id="page1">, <body id="page2">, <body id="page3">, <body id="page4">
    It works for the main page, I have <body>

    but for all other pages I don't have <body id="defaultid">, i.e. if my page is 'somepage' I want <body id="defaultid">, but my code returns me
    <body id="somepage">

    What is wrong with my if/else?

    Thanks

  2. Odai Athamneh
    Member
    Posted 1 year ago #

    This is just a thought, but I was reading the Codex page for is_page, and I noticed the examples use arrays for multiple is_page evaluations. Might be worth a try.

    So instead of:

    elseif (is_page( 'page1'||'page2'||'page3'||'page4' ) ) {

    you would have

    elseif (is_page( array ( 'page1', 'page2', 'page3', 'page4') ) ) {

  3. natpoit7
    Member
    Posted 1 year ago #

    thank you very much, it does work

    Regards,
    Nathalie

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.