1

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:

<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

3 Answers 3

1

Try this.

if($(this).parent().hasClass('work_section')){
  // your code
} else {
  // your code
}
Sign up to request clarification or add additional context in comments.

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();
});

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.