Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am a bit confused on how this may be achieved. I have a website that I have coded already with static HTML/CSS and I am trying to incorporate it into wordpress. The challenge though is that the website is single page index.html file with 4 "sections". I want each section to be a page that can be accessed by anyone to edit the content at a later date. I have figured out how to get the page to loop out my four pages through other stackoverflow resources, however the challenge I am facing is the HTML markup.

Is there a way to make each section loop out different HTML for that given section? Each section is formatted differently.

For example, section one may have 4 divs, while section three has one div and it is a different ID. The current loop only will loop out the same kind of HTML.

I've read about custom fields which I plan to incorporate for specific parts of the sections, but from a wider scope, I can't figure out how to get different HTML markup for each section first.

<?php $pages = get_pages();
  foreach ($pages as $page_data) {
  $content = apply_filters('the_content', $page_data->post_content);
  $title = $page_data->post_title;
$slug = $page_data->post_name;

echo "<div id='$slug'>";
echo "<h2>$title</h2>";
echo $content;
echo "</div>";
}
?>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

You could attack the get_pages(); function another way to sort your code better and read it better as well, take a look at this link for more info.

<?php $args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'post_title', // sorting by alphabetical order of post title for example
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',    // edit this parameter instead of $pages = get_pages('include=2,4,5,8');
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
); //edit whatever parameter you need to get what you need (sort_column to select which column to sort by), this list shows all parameters!!!

$pages = get_pages($args); 

    foreach ($pages as $page_data) {
        $content = apply_filters('the_content', $page_data->post_content);
        $title = $page_data->post_title;
        $slug = $page_data->post_name;

        if( $page_data->ID == 2 ){ //format 1
            get_template_part( 'content-1', get_post_format() ); //get post template from content-1.php
            }
        elseif( $page_data->ID == 4 ){ //format 2
            get_template_part( 'content-2', get_post_format() ); //get post template from content-2.php
            }

        }
?>

I presented a variation for the content of the if statement in case the code that displays each section gets too big, you can simply break it into other files to control it with ease and make it more readable. More usage about the get_template_part(); function refer to this link.

share|improve this answer
    
Why is it that if I pass nothing as a parameter to include ('include' ==> '') and then in the if statement, for testing purposes just have if( $page_data->ID = 2 ){ //format 1 echo $content; } It echos out every page I have. However if I put 2 as the parameter in the include, and change the if statement to ->ID == 3. Page 2 still shows? –  James May 22 '13 at 15:26
1  
I see why, it needed to be $page_data->ID == 2, not = 2 –  James May 22 '13 at 16:23
    
Ok, I'll correct that on my answer, but don't forget I posted all the possible arguments in the $args array, so you can clear whichever you aren't using. –  Fernando Silva May 22 '13 at 16:46
1  
This is a great help, I'm on my way so far to building my static one page now into wordpress. I'll let you know how the rest goes! –  James May 22 '13 at 17:31
    
@James By the way I'm also trying to build a "one page" template but based on categories if you want to take a peek at the loop, it might give you ideas link –  Fernando Silva May 22 '13 at 18:49
add comment

well you can have conditional if else statements to output different html for different pages, based on page id. But first modify your get_pages to fetch only those 4 pages(or 'sections' as you are calling it). The current code will show all the pages that are there on your website. I am pretty sure you don't want that. so instead of just

$pages = get_pages();

you should do something like

$pages = get_pages('include=2,4,5,8');

where 2,4 etc.. are the id of desired pages. Then, you can modify your code to check for page id and output html accordingly

if( $page_data->ID == 2 ){
//format 1
echo "<div id='$slug'>";
echo "<h2>$title</h2>";
echo $content;
echo "</div>";
}
elseif( $page_data->ID == 2 ){
//format 2
echo "<div id='page-". $page_data->ID ."'>";
echo "<div class='page-title'>$title</h2>";
echo $content;
echo "</div>";
}
//... and so on
share|improve this answer
    
Thank you, this makes a lot of logical sense reading this response. I have a few questions though. Is there a way to make the pages show in a given order? I ordered them under the admin side but that did not seem to effect anything. Also when I try to put your code in, nothing appears on the page. When I add $pages = get_pages('include=2,4,5,8'); mixed with my orignal code, it shows just those pages but the if part of yours simply shows blank when I test it. –  James May 21 '13 at 17:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.