I am new to wordpress and wordpress loop. I am trying to understand the loop but without any success. Baer with me I will try to explain what I am not understanding …
I using a template called ‘graphy’. When I create a ‘Page’ there is an option to create a page with no side bar the template is called ‘nosidebar.php’ and this is its code:
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
1- Why this template contains a loop ? where it only displays a single page content without side bar ! Obviously it is not looping through posts and displaying them !
I tried to create my own template page which will be used only for front-page and here is what I came up with
<?php
/**
* Template Name: Main_Page
* Description: A page template without sidebar.
*
* @package Graphy
*/
get_header();
?>
<!--<div id="primary" class="content-area">-->
<!--<main id="main" class="site-main" role="main">-->
<div id="main_content">
<?php
the_content(); // the content
?>
</div>
<!--</main> #main -->
<!--</div> #primary -->
<?php get_footer(); ?>
However when I installed this plugin which is used to insert widgets to pages and posts
with the main_page no widget is displayed but when I switched to “no sidebar page” it worked.
I then copied the loop into my main page and it worked.
2- What is the secret that this loops makes the plug-in work, while calling only <?php the_content() ?>
does not ?! Obviously this loop makes some other things than what 90% of the posts on the internet explain.
On your first question, page templates does output information, that is why you see the loop. This information that is shown is the information entered in the page editor screen inside the tinymce editor.
For better understanding, go and read these two posts I’ve done recently on WPSE
Guidance with The Loop for CMS
The Loop in Static Page
On question two,
the_content()
does not output sidebars and widgets, but the content entered into the post editor. Sidebars are displayed with specific calls.You will need to go and look how your theme register sidebars. I also suspect that your sidebar’s behavior is manipulated by
body_classes
. Unfortunately here I can’t help as this is quite very specific to your themeIts all about
the_post();
,there is no need for any while loop there.Try this
It will work.
the_post()
is the function that retrieves a post content.The while loop is needed only when retrieved are called from a category.