Ask your WordPress questions! Pay money and get answers fast! (more info)

I need a loop

  • REFUNDED

I am working on a one page WordPress website. I need all posts and pages brought into the front page. I am using a page template for the front page and I am using the 2014 theme as the backbone for the site. I would like it so that only posts and pages that show up in the navigation are pulled onto the site. My current loop does not do this

so in my site, every post and page would be loaded like this



<li id="page=about_page">
the page content goes here
</li>


One of the problems I have is that my loop doesn't get the page templates. So of I created a page called "About" and assigned a page template to that page called "template-about.php" and put a bunch of custom html in that template, the loop would only get the content from the page editor in WordPress and not the html from the template.

Here is the current loop I came up with



$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>

//Here I am checking if there is a page template assigned to the page
<?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?>

//If there is a template assigned and the post content isn't empty, then load the page.
<?php if( $template && $post->post_content != '' ) { ?>

//Here I am using the title to put in the page ID. This is how the javascript will scroll the page into view
<li id="page=<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">

//Here I am trying to get the page content
<?php get_template_part( 'content', 'page' ); ?>

</li>
<?php }

endwhile;
endif;
?>


In the code, I made a page template for all the pages so that only those pages that have a template assigned would load into the slider system for the one page theme. I also have it so that you could link to the page normally by reloading the page. This is how the blog works.

So this is how it would work. You click the link in the navigation. The page slides into view from the left. When you click the next link, the page leaves out the left and the new page slides in. I have all this working. All I need is a better loop to grab the page content.

I have tried using get_posts, get_pages, get_page_templates.

For example. This loop does get the content from the custom template but for some reason, when I run it. The footer displays before the content. I think that is because it is loading the template-home.php file twice.


$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>

<?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?>

//Trying to load all page templates accept the homepage
<?php if( $template && $template != 'template-home.php') { ?>
<li id="page_<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
<?php
$file = get_page_template();
require_once($file);
?></li><?php
}
endwhile;
endif; ?>


I was messing around with the loop above. So it isn't perfect.


I don't need a loop that slides everything. All I am asking for is a loop that loads all posts and pages into the frontpage of WordPress.

Answers (6)

2014-04-09

Sarbartha Das answers:

I will help you out. PM back!

2014-04-09

PHPSmashCode answers:

Hello,

In your code first set of code, you are calling template file using
<blockquote><?php get_template_part( 'content', 'page' ); ?></blockquote>
it won't call a file named template-about.php or template-home.php. It will only call the file named content-page.php.
So you need to make it dynamic based on
<blockquote><?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?></blockquote>

that is, if $template have the value "about". The code should be like this

<?php get_template_part( 'template', $template ); ?>

Then it will call the file template-about.php

Check this link for details
https://codex.wordpress.org/Function_Reference/get_template_part

Let me know if you need more help.


Jamie comments:

The $template variable does hold the name of the template files; however, when I make the change your suggest. I get nothing. Only the page titles show up. When I do a print_r($template); I get all the names of my templates. ex... template-about.php

So my page now looks like an unordered list with no content.


PHPSmashCode comments:

Oh. Okay.
Here you can call it like this

<?php get_template_part( 'template', str_replace(' ','_',strtolower( get_the_title()) ); ?>


Jamie comments:

that isn't valid code. It gives me an error and the page doesn't load.


PHPSmashCode comments:

Try this
<?php get_template_part(basename($template)); ?>


PHPSmashCode comments:

The code would be like this
$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
//Here I am checking if there is a page template assigned to the page
<?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?>
//If there is a template assigned and the post content isn't empty, then load the page.
<?php if( $template && $post->post_content != '' ) { ?>
//Here I am using the title to put in the page ID. This is how the javascript will scroll the page into view

<li id="page=<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">

//Here I am trying to get the page content

<?php get_template_part(basename($template)); ?>

</li>

<?php }

endwhile;

endif;


Jamie comments:

Changing one line of code isn't going to do it. This doesn't work. I get the same thing. An empty page of result where only the title of the page is displayed


<li id="page_sample_page">

</li>
<li id="page_discography">

</li>
<li id="page_images">

</li>
<li id="page_news">

</li>
<li id="page_shows">

</li>
<li id="page_the_band">

</li>
<li id="page_videos">

</li>
<li id="page_contact">

</li>
<li id="page_about_us">

</li>
<li id="page_columns">

</li>
<li id="page_column_scroll">

</li>
<li id="page_widgets">

</li>
<li id="page_page_builder">

</li>
<li id="page_wordpress_gallery">

</li>
<li id="page_test">

</li>


What I am looking for is almost a complete new loop that gets all posts and pages including page templates.


PHPSmashCode comments:

Can you show the code that written in template-about.php


PHPSmashCode comments:

Do you have separate php template files for all these wordpress pages?


PHPSmashCode comments:

You can only call get_template_part() If you have separate php files for all pages. If you don't have separate template files, you need to call the_content() in loop.


Jamie comments:

Each template in my theme has a loop in it designed to get content from a category. My original loop posted above does not get the templates correctly. So instead of displaying that content, it displays the content from the page editor as I said in my original post. This is why I am searching for a new loop. I have had to use shortcodes in order to get my queries to run against the categories since the page templates they are in are not getting pulled correctly.


PHPSmashCode comments:

Let me know is it working with following code?


$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
//Here I am checking if there is a page template assigned to the page
<?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?>
//If there is a template assigned and the post content isn't empty, then load the page.
<?php if( $template && $post->post_content != '' ) { ?>
//Here I am using the title to put in the page ID. This is how the javascript will scroll the page into view
<li id="page=<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
//Here I am trying to get the page content
<?php the_content(); ?>
</li>
<?php }

endwhile;
endif;


Jamie comments:

all you guys are doing is taking my loop and changing one line to the_content. I have done all that. It doesn't work. Maybe there isn't a solution for this issue


PHPSmashCode comments:

Loop that you created seems correct. I think the only issue is in displaying content. Right?

[Your loop out putting the content (Check http://hastebin.com/ronimovuke.xml your loop out put you mentioned previous post). It only missing the content.]

I still didn't get why you need a new loop.

Let me know if missing something about understanding your requirement?

2014-04-09

Dbranes answers:

Notice that <em>get_page_template()</em> gives you the current template, but the post meta <em>_wp_page_template</em> isn't automatically updated when you switch themes, so it can keep data of old templates.

You should compare these two values in your debug.

So try for example:

get_template_part( basename( get_page_template(), '.php' ) );


to include the custom page template.

You don't need paging:

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1


when you are fetching all posts with:

'posts_per_page' => '-1',


Usually <em>posts</em> don't have assigned custom page templates, so why do you want to query <em>posts</em> as well as <em>pages</em>?

<strong>Update 1:</strong>
You could try some debug loop to start with:

$page_query = new WP_Query( array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
));

if ( $page_query->have_posts() ) :
while ( $page_query->have_posts() ) : $page_query->the_post();

// debug:
printf( 'get_the_title(): %s <br/>', get_the_title() );
printf( '_wp_page_template: %s <br/>', get_post_meta( get_the_ID(), '_wp_page_template', TRUE ) );
printf( 'get_page_template(): %s <br/>', get_page_template() );
printf( 'get_post_type(): %s <br/>', get_post_type() );
printf( 'basename(): %s <hr/> ', basename( get_page_template(), '.php' ) );

// include:
// get_template_part( basename( get_page_template(), '.php' ) );

endwhile;
endif;


just to get some idea what is going on there.

<strong>Update 2: </strong>

I created a minimal one-page theme with only 3 files, <em>index.php</em>, <em>tpl-car.php</em> and <em>style.css</em>.

I created a single page called 'Ferrari' with the slug 'ferrari'.

It was assigned to the custom template 'tpl-car.php'.

---
<em>index.php:</em>

<?php
//get_header();

$page_query = new WP_Query( array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'name' => 'ferrari',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
));

if ( $page_query->have_posts() ) :
while ( $page_query->have_posts() ) : $page_query->the_post();

// debug:
print( '<hr/><h3>Debug:</h3>' );
printf( 'get_the_title(): %s <br/>', get_the_title() );
printf( '_wp_page_template: %s <br/>', get_post_meta( get_the_ID(), '_wp_page_template', TRUE ) );
printf( 'get_page_template(): %s <br/>', get_page_template() );
printf( 'get_post_type(): %s <br/>', get_post_type() );
printf( 'basename(): %s <br/> ', basename( get_page_template(), '.php' ) );
printf( '<h3>get_template_part("%s"):</h3>', basename( get_page_template(), '.php' ) );
// include:
get_template_part( basename( get_page_template(), '.php' ) );

endwhile;
endif;

//get_footer();



<em>tpl-car.php:</em>

<?php
/*
Template Name: Car
*/
?>

<?php echo 'Testing the template "tpl-car.php"'; ?>

<article>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>


This works and gives the following output:

Debug:

get_the_title(): Ferrari
_wp_page_template: tpl-car.php
get_page_template(): /path/to/wp-content/themes/onepage/tpl-car.php
get_post_type(): page
basename(): tpl-car
get_template_part("tpl-car"):

Testing the template: tpl-car.php
Ferrari

This line is from the content editor and contains description of the Ferrari car!


I wonder if you can replicate this for your problematic page ?


Jamie comments:

I want to query posts and pages because I am building a one page WordPress site. I need all posts and pages to load on the front page of my site so that I can cycle through them with the slider system that is in place. My problem is that my loop doesn't get the posts and pages the best way. I was hoping that someone here would be able to write up a new loop pretty quick that would solve the issue. I feel like a pro could wip one out in less than half an hour. Maybe I am wrong.


Dbranes comments:

I added a debug loop for you to test, hopefully that can give you some useful info.


Dbranes comments:

So we can take this step by step:

Now when you have the list from the above debug loop,

the next step would be to see what items are missing from the list

and what items you don't want.

So we can start by adjusting that.




Jamie comments:

My loop works. My real issue is that it does not get the actual page template. If I create a page called test and assign a page template to it called template-test.php and then in that template I do a php echo that says "Testing the template". When that page loads, testing the template will not be there. The section would be blank. But if I go into the backend of wordpress and type something into the editor for that page, then the content will show. Maybe I should just use a page builder and forget about it.


Dbranes comments:

I created a minimal them to test this and it worked ok.

I updated the answer to describe it better.

I wonder if you can replicate this for your problematic page? i.e. just test a single page to start with.

ps: also what happens when you include it by hand within your loop, like the following:

include( '/path/to/template-test.php' );

or

get_template_part( 'template-test' );

where you restrict the loop to only a single item, assigned to this template, like I did in my latest update?


Jamie comments:

the loop you made does get the page template. But just that one page. That is no good. The loop needs to get all posts, pages and page templates for the homepage. I don't want to assign them manually. That would defeat the purpose of WordPress.


Dbranes comments:

This is part of the debugging:

Break the problem down into smaller pieces. Then find a piece that works and expand from that.


Good to hear that this is working on single items. I then assume that this:

<blockquote> If I create a page called test and assign a page template to it called template-test.php and then in that template I do a php echo that says "Testing the template". When that page loads, testing the template will not be there. The section would be blank.</blockquote>

is not a problem for a single item?

Next step would be to expand it a little bit and play with these parameters:

'posts_per_page' => 2,
'offset' => 1,


if this works for 2 items, with various offsets, then let's try for example:

'posts_per_page' => 10,
'offset' => 1,


if this doesn't work as expected, then let's decrease it to 5, etc.


Dbranes comments:

When we find the setup that breaks, when we expand again from single items,
we should be able to get important information from the <em>debug-printf</em> part in the loop.

ps: can you share the <em>template-test.php</em> template? Just so we can see if it contains extra loops, output buffering, etc


Jamie comments:

Ok. My template test is made up of nothing but this



<?php /* template name: test */ ?>

<div id="testing">
<?php echo 'testing the test page'; ?>
</div>


When I include it as you asked with a simple

get_template_part( 'template-test');


It works. It does get the template code. That is a good thing. So now I am thinking that if I make the loop so that it will test if a page has a template then it can include it with the

get_template_part( $template ) else

get_template_part('content', get_post_format());

so that should bring in all the templates as well as posts.

So can you help me figure out this out for a final code. I think including the template using get_template_part will work. I already have the code above to get the template name using

$template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );

Just need to put it all together in a loop and toss in this bit of code


This should make it so that only the posts and pages that are in the menu will show on the front page. Just not sure how to put it all together

$args = array(
'menu' => 'mainmenu',
'menu_class' => 'mainmenu',
'container' => false,
'menu_id' => 'menu',
'menu_class' => '',
'menu_id' => 'menu',
'container' => false,
'theme_location' => 'primary',
'show_home' => false,
'walker' => new Kiss_Custom_Walker_Nav_Sub_Menu,
'echo' => 0
);

$menu_code = wp_nav_menu($args);
$menu_code = str_replace('<ul class="sub-menu">','<div class="submenu_1"><ul>',$menu_code);

echo $menu_code;


if (($locations = get_nav_menu_locations()) && $locations['primary_navi'] ) {
$menu = wp_get_nav_menu_object( $locations['primary_navi'] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$pageID = array();
foreach($menu_items as $item) {
if($item->object == 'page')
$pageID[] = $item->object_id;
}
query_posts( array( 'post_type' => 'page','post__in' => $pageID, 'posts_per_page' => count($pageID), 'orderby' => 'post__in' ) );
}
while(have_posts() ) : the_post();
//Loop here.
endwhile;


Dbranes comments:

ok great.

You could try to replace this line:

// Loop


with:

$tpl = basename( get_page_template(), '.php' );

if( 'page' === $tpl ){
$tpl = sprintf( 'content-%s', get_post_type() );
}

// include:
get_template_part( $tpl );


but it's getting very late here so I will check again tomorrow ;-)

2014-04-09

Arnav Joy answers:

Can you try
<?php the_content(); ?>
In place of

<?php get_template_part( 'content', 'page' ); ?>

2014-04-09

S├ębastien | French WordpressDesigner answers:

this code :
$template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );
return something like template-about.php
but in the function get_template_part($template); you need a variable $template like template-about (not template-about.php)

So try this code :


<?php

$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));


if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
<?php
$template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );
if( $template && $template != 'template-home.php') { ?>
<li id="page_<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
<?php
//here we remove ".php"
$template = str_replace('.php','',basename( get_page_template() ));
get_template_part($template); ?>
</li><?php }
endwhile;
endif;
?>


Jamie comments:

Something about this is wrong. When I try this solution, it puts the content after the footer.


Jamie comments:

It also does not get the templates.


Jamie comments:

Sorry, One more edit. You code as above does get the templates. The only error after all is that it puts the content in teh wrong place. It is following the footer instead of coming before it.


S├ębastien | French WordpressDesigner comments:

OK :-)
The templates are displayed, therefore your original problem seems resolved.

I tried on my site. The content is displayed on the good place.
I think the problem comes probably not from my code.
I suppose there is a problem with your template page.

We need to verify where the problem come from.
First : save a copy of the template displaying your homepage.

You can eventually create a new template with this content :
<?php
/*
Template Name: Test Template
*/
?>
<br>
<div style="width:250px;background:#eee;margin:10px;padding:15px;border:#ddd">
<?php
the_title();
echo "<br>";
the_content();
echo "<br>";
?>

</div>

Save this file in your theme as test-template.php
Choose this template for all your pages.

Now, replace the code displaying your home by
<html>
<head>
<title>test</title>
</head>
<body>
here is the top of the page
<br>
<?php
$page_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => array( 'post', 'page' ),
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => '-1',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
<?php
$template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );
if( $template && $template != 'template-home.php') { ?>
<li id="page_<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
<?php
//here we remove ".php"
$template = str_replace('.php','',basename( get_page_template() ));
get_template_part($template); ?>
</li><?php }
endwhile;
endif;
?>

here is my footer
<br>

</body>
</html>


if the homepage displays each page with my test template, and the footer is in good place, the problem don't come from my code.
And your question is resolved :-)


Jamie comments:

the code seems to be working. I need to do more testing to find out. It's weird that this code gets page.php which calls content-page.php. I wouldn't think that it would do it. I found that it was the call to the footer in page.php which is what was causing my problem with the code. So I added a conditional to get rid of the header and footer if it's the front page. Seems to be working.

last part and this could be solved..

can you help me figure out how to get the code from this site to work in the loop?

http://forrst.com/posts/Build_an_one_page_portfolio_website_with_WordPre-FAt

I want to make it so that only the pages that are in the navigation are pulled by the loop.


Jamie comments:

I spoke to soon. The loop isn't working correctly. It's duplicating content. For example...

I created a page called home and assigned the template-home.php to that page and set the setting in the reading tab for a static homepage using that home template. I have 15 pages including the homepage and the loop is displaying the home page 11 times. So the only pages in my site right now are the 11 home pages and the one test template. Here is the code from my site with the loop

$page_query = new WP_Query(array(

'post_status' => 'publish',

'post_type' => array( 'post', 'page' ),

'order' => 'ASC',

'orderby' => 'menu_order',

'posts_per_page' => '-1',

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1

));

if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>

<?php

$template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );

if( $template && $template != 'template-home.php') { ?>


<?php

//here we remove ".php"

$template = str_replace('.php','',basename(get_page_template()));

get_template_part($template); ?>


<?php }

endwhile;

endif;




Here is the output
http://pastebin.com/HTydvP6Y


If you check the title of the pages. Almost all of them say "home"


Jamie comments:

I think I came up with solution. The problem I found with your loop above is that it was including the default template which caused it to call page.php. This page was causing multiple issues in my loop. So I added a check in the conditional to cut out the default template. Now it works. So then, I will make a template-page.php for the pages that will be added to the one page system. This should allow regular pages to work outside of the one page loop. So here is my full loop now.


$page_query = new WP_Query(array(

'post_status' => 'publish',

'post_type' => array( 'post', 'page' ),

'order' => 'ASC',

'orderby' => 'menu_order',

'posts_per_page' => '-1',

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1

));

if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>

<?php

$template = get_post_meta( $post->ID, '_wp_page_template', TRUE );

if( $template && $template != 'template-home.php' && $template != 'default') {

var_dump($template);

//here we remove ".php"

$template = str_replace('.php','',basename(get_page_template()));

get_template_part($template); ?>


<?php }

endwhile;

endif;


I would be interested to see if you have any other improvements?


Jamie comments:

Na, it's not working. The posts won't display properly and for some reason, it seems to work and then fail. When I try adding the content for the posts. I get the homepage repeated multiple times

2014-04-09

Bob answers:

can you provide url of your website?

In short you need all pages or post set in navigation menu.

did you done different design for different page templates?

it is completely vertical scrolling or horizontal scrolling also?

we have recently created one website which which fetch data as per navigation menu created.
means whatever is set in navigation menu it will get content according to it
[[LINK href="http://avayaplus.com/singlepage2/"]]http://avayaplus.com/singlepage2/[[/LINK]]

let me know if i am misunderstanding.