PHP if child of display this HTML, else display that HTML

First question on here. I am doing a custom WordPress site and I need to divide the post page (single.php) so that if it is a child of my WORK page it displays a certain HTML, else it displays another chunk of HTML.

At the moment I have the following inside the single.php file:

Read More
<div class="work_section">
<p>WORK SECTION GOES HERE</p>
</div>
<!--div:work_section -->

<div class="news_section">
<p>NEWS SECTION GOES HERE</p>
</div>
<!--div:news_section -->

i am trying to tell it to display one if it’s a child of WORK page, or else the other div… how would I go about doing this?

Many thanks

Related posts

Leave a Reply

3 comments

  1. You can simply hide/show div elements, for example, on a button click. If you have two buttons with a data attribute, you can easily switch between these two pages. Buttons HTML:

    <div class="button" data-section="work">Work</div>
    <div class="button" data-section="news">News</div>
    

    And the jQuery:

    $(".button").click(function() {
        $(".work_section").hide();
        $(".news_section").hide();
        var section=$(this).data("section");
        $("."+section+"_section").show();
    });
    

    But it can be more precise and readable if you use a section element insted of a div, with shorter class names like “news” and “work”. Then your jQuery code will be shorter:

    $(".button").click(function() {
        $("section").hide();
        $("."+$(this).data("section")).show();
    });
    
  2. There are several ways of doing this in wordpress.

    if ( is_page(2) ) {
     // stuff
    }
    

    or:

    if ( $post->post_parent == 'WORK' ) {
      // stuff
     }
    

    But I am not quiet sure if thats the best way to go about it. Maybe you should rethink how you are are modelling the site.

    I would suggest to create 2 different custom post types, work & news. This way you can create instances or posts for each type e.g. single-work.php or single-news.php.

    Codex: Custom Post Types

    Another way would be to make WORK a category and do custom loop/query and/or conditional statements.