• My theme uses the following code as part of the custom post type template:

    <div class=”main-content<?php if($reviewSidebarEnabled) { ?>-left<?php } else { ?> full-width<?php } ?>”>

    I would like to keep this code but wrap it with another IF statement based on the value of a custom field. i.e.

    IF custom_field_value = “full” then

    <div class=”main-content-full-width”>

    else

    <div class=”main-content<?php if($reviewSidebarEnabled) { ?>-left<?php } else { ?> full-width<?php } ?>”>

    end if

    Is this doable? I will need some help with the actual syntax.

Viewing 5 replies - 1 through 5 (of 5 total)
  • So, right now the code gives you two different layout options based on whether or not your $reviewSidebarEnabled variable is true.

    1. Option 1: <div class="main-content-left">
    2. Option 2: <div class="main-content full-width">

    (Notice there is NOT a dash between main-content and full-width in the second option there.)

    And what you WANT to do is add a third option based on whether or not a new variable called custom_field_value is set to "full" or not, right?

    The original two choices would remain, but you want to add a third option that would take precedence over the other two like so:

    1. Option 0 (NEW): <div class="main-content-full-width"> WITH a dash
    2. Option 1: <div class="main-content-left">
    3. Option 2: <div class="main-content full-width">

    I don’t really understand why you’d want to allow yourself to get confused by two very similar classes: "main-content-full-width" and "main-content" with "full-wdith" might get confusing. I’d recommend changing your new custom_field_value class to something simpler, like <div class="main-content-full">.

    Anyway, if you want to do that, this code should probably do the trick:

    <?php if($custom_field_value === "full") { ?>
        <div class="main-content-full">
    <?php } else { ?>
        <div class="main-content<?php if($reviewSidebarEnabled) { ?>-left<?php } else { ?> full-width<?php } ?>">

    You basically wrote it out yourself already.

    Thread Starter maraki

    (@neodjandre)

    yes that’s what I want exactly… i just don’t know how to code it in PHP..

    The code I gave you there at the end of the first comment should work:

    <?php if($custom_field_value === "full") { ?>
        <div class="main-content-full">
    <?php } else { ?>
        <div class="main-content<?php if($reviewSidebarEnabled) { ?>-left<?php } else { ?> full-width<?php } ?>">

    Did that work?

    Thread Starter maraki

    (@neodjandre)

    yes it worked thanks !

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Custom field in template’ is closed to new replies.