Why do wordpress theme loops in a single page - php

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 theme

Its all about the_post(); ,there is no need for any while loop there.
Try this
<?php
the_post();
the_content();
?>
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.

Related

Wordpress included custom PHP page but use styling of current theme?

I have a number of individual page with custom php and javascript that I need to use in a wordpress site.
I had read one way to do this would be to create a custom template and 'include' the page, this works, but I dont get the basic styling of a Wordpress generated page.
eg:
page-testa-php.php (simple example) :
<?php
echo "This is a line of my content!";
?>
Published page uses template below:
<?php
/**
* Template Name: TestA
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
get_header(); ?>
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
include('page-testa-php.php');
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
The text 'This is a line of my content!' does show in the correct area of the page, but I can already see it is not subject to the styling that would occur if I added the text directly into the page via the Wordpress editor.
(it has no indenting and is hard left against the left hand sidebar for example)
I was hoping to be able to add styling markup in my own code like 'text-align: center;' etc. but have that working within the control of the themes responsiveness design. Is this possible?
Is it just that I have the 'include' in the wrong place in the template file?, or that I need some basic markup in my included file to 'access' the styling of my theme?
I can't find anything that documents the structure of a Wordpress page that tells me what classes I would need (or can) reference in source page.
Try using some formatting tags, e.g. p, h3, etc. Is it still not pulling your theme styles?

Custom Page Templates Not Displaying In Dropdown

I'm working on a One-Page custom Wordpress theme him having some problem in the Backend of the WordPress. When every I make a Custom Page Template in my directory does not show up in dropdown I have added everything and save everything right insides the code.
This how him loading all the custom pages, header, and footer to load on front-page.
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('templates/page', 'header'); ?>
<?php get_template_part('templates/modules/content', 'page-about'); ?>
<?php get_template_part('templates/modules/content', 'page-services'); ?>
<?php get_template_part('templates/modules/content', 'page-photography'); ?>
<?php get_template_part('templates/modules/content', 'page-portfolio'); ?>
<?php get_template_part('templates/modules/content', 'page-contact'); ?>
<?php get_template_part('templates/page', 'footer'); ?>
<?php endwhile; ?>
</main><!-- #main -->
</div><!-- #primary -->
Here are some screenshot of directory of custom pages that trying to load inside my dropdown template.
Page 1 https://gyazo.com/b5cb0349b68c9819cf4632b238201259
Page 2 https://gyazo.com/88baa34089370362c77edc2b67f12617
Page 3 https://gyazo.com/12f204187c92c7f2ec380845ee2490dc
Page 4 https://gyazo.com/2b05906194a6e4ee7c0eacf5d19b7bef
Page 5 https://gyazo.com/af8f59a00c68008eb833d8420a38961f
Backend https://gyazo.com/455f44b60d102856ea56cf2ad00adbb4
you are making one page theme and you created some templates but i can see in screenshot that you are calling all templates under home page so there is already header and footer included then why you are adding header and footer to all the other templates?
on one page theme you just don't need to include header and footer to every template

Creating a custom template page in wordpress causing other features to stop working

I'm new to wordpress and i'm trying to create a custom template. I'm using the default twentyfifteen theme. I went to wp-content/themes/twentyfifteen and copied page.php to a new file called page_with-contact.php and added this comment on the top :
/*
Template Name: Page with contact
*/
I made no other changes to the template.
I then went to the admin site and changed one of the pages to "page with contact".
When I open the page I see that the affix on the left menu is not working and the responsive menu is not working either.
I followed a pretty simple tutorial here so I'm just wondering what am I doing wrong.
EDIT
Following #masiorama's answer and the comments below is crated a child theme, moved the template file to the child theme and renamed it to page-with-contact.php, This is the content of the template
<?php
/*
Template Name: Page with contact
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main my-content-page" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
?>
<div class="hentry entry-content contact-form">
<?php
echo do_shortcode('[contact-form-7 id="19" title="contact form 1"]');
?>
</div>
<?php
// End the loop.
endwhile;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php
get_sidebar();
get_footer();
?>
Now I have several problems :
As you can see the sidebar, heading and footer are all included but the affix and responsive menu are still not working.
The contact form (which previously worked fine) is now not showing at all.
I'm unable to enqueue the parent's rtl.css file.
Appreciate any further guidance.
As far as I know your page file should be named:
page-with-contact.php
and not:
page_with-contact.php
Be sure that it contains at least some wordpress function calls like:
<?php get_header(); ?>
<!-- stuff -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Check this for more details: http://codex.wordpress.org/Page_Templates
Looks like you are calling a template within a template. 'get_template_part( 'content', 'page' );' should be the_content();. Good chance that's whats messing you around.
Need this WordPress hooke to call in your template
get_header();
get_sidebar();
get_footer();

Why this Wordpress page.php file contain the posts loop?

I am pretty new in WordPress theme development and I have the following question about the page.php page contained into templates files (the file that describe the template for the static pages)
I am analazyng the page.php file contained into the twentythirten default theme, the code of this file is:
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that other
* 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Thirteen
* #since Twenty Thirteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
My doubts are:
1) page.php is the default template for the static pages but this file contain the posts loop:
<?php while ( have_posts() ) : the_post(); ?>
Why?
WordPress is designed so that a very basic theme only needs one template: index.php. If you create a theme with an index.php, that file will be used for rendering all your content: the archives, the main blog page, individual pages, and so on. And all it needs on it to basically work is the loop.
To achieve this, virtually every piece of content in WordPress is technically a "post". Blog posts are posts. Individual pages are "posts". Even search results are "posts". And all you need to do to display them is loop through the objects that WordPress hands you and output them.
Obviously, it's more helpful if different types of posts are displayed in different ways, so WordPress provides ways of overriding this default behaviour. If you add a page.php template, then WordPress will invoke that for individual pages instead of the default index.php. See the Template Hierarchy diagram to see which template page will be used for a given type of post. (But note that they all fall through to index.php if no specific template exists.)
Because of this, you'll normally see a "post" "loop" used for any given template type in WordPress, even if you're not actually going to loop more than once, and even if you're not actually outputting blog posts.
On a single Page, WordPress will only "loop" once at most, and you can probably assume that you'll always have one post to output, so if you really want to, you can replace the usual while loop with code to output a single post. However, because WordPress still hands you a "set" of posts that just happens to contain a single post, you still need to at least call the_post() to load up this single post into the variables that are used for the later output calls like the_content(). As the code is therefore virtually identical with a loop, and most people have already written the loop for their index.php template, normally people just leave it as a loop, knowing it'll only execute once.
If you're developing a theme, you'll want to get very familiar with how the template hierarchy and loop work together to output WordPress content.
WordPress has many types of 'post'. The default 'post types' are page, attachment, nav_menu_item, post and revision.
You can even create your own post types using the register_post_type() function. Post types registered in this way are called 'custom post types'.
Refs:
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Post_Types#Custom_Post_Types
http://codex.wordpress.org/Function_Reference/register_post_type

Custom template not working with the following options

I followed this tutorial for the page specific template -- http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
Created a page through wordpress admin panel - Blog Page URL like -- http://localhost/wordpress/blog-page/ and set template to my template "Swapnesh" from the admin panel itself.
Created my specific page template as page-blog-page.php containing following code --
<?php
/*
Template Name: Swapnesh
*/
get_header(); ?>
<div id="primary">
<div id="contentabc" style="border:7px solid red;">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
</div><!-- #primary -->
<?php get_footer(); ?>
Now when im navigating to http://mysite/wordpress/blog-page/ im not getting that border so that i can proceed further, let me know what im doing wrong.
note-- Under "Reading Settings" mu post page selection was "Blog Page" when i dis select this option its showing me the red border but no posts then :(
I don't know if you've solved this yet but I think the problem relates to the belt and braces approach you've ended up with here. By naming your custom template page-blog-page.php it should negate the need to explicitly assign it as a custom template for a page with the slug blog-page.
Try changing the name of the custom template completely - my-template.php - to see what happens. I don't see anything obviously wrong with your code.
Have you selected Swapnesh as page template While creating new page ?
You can select it from right side Page Attributes.

Categories