Link wordpress page to php file not working - php

I got a wordpress page called "Galerie". I'd like to display some content on this page. So I createda php file called "content-galerie.php" and wrote some content in it.
But the content is not displaying at all. It only displays the default page with the header, the title and the footer.
How can i fix that? So my content gets displayed?
For example a simple Text
<p>Lorem ipsum dolor sit amet consetetur sadipscing...</p>

You have to create Page template for gallery section.
<?php
/**
* Template Name: Gallery
*
* 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_Sixteen
* #since Twenty Sixteen 1.0
*/
get_header();
?>
// You can write your gallery content here
<section class="section single-page">
<div class="fixed_container">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/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();
}
// End of the loop.
endwhile;
?>
</div>
</section>
<?php get_footer(); ?>
After that edit you gallery page from admin and assign this template to your gallery page.

If you have created "content-galerie.php" page in theme folder then you can that page like this.
get_template_part('content-galerie');

Related

Wordpress: cannot see content in page.php, only sidebar

I've created my own theme and copied and pasted the code from other standard Wordpress themes into my page.php but it won't show the content, only the sidebar if the code calls for it.
An example of the code for page.php that I copied from Twentyseventeen:
<?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 may use a
* different template.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Seventeen
* #since 1.0
* #version 1.0
*/
get_header(); ?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/page/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();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
<?php get_footer();
I have created many different custom page templates already and they all display the content just fine.
I've created a very standard new page with the default template (page.php) and added text in the admin panel (so not directly to the template) but this text, or any shortcodes or other input, does not get displayed when viewing the finished page.
Can't figure out what is missing. Any help would be very much appreciated, thanks.
I assume you don't have the file content-page.php in template-parts/page? If not, either copy and paste the whole template-parts folder from the Twentyseventeen theme or replace the line ' get_template_part( 'template-parts/page/content', 'page' );' with 'the_content();'

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?

wordpress theme breaks after inserting external php ? possible mistake in folder config?

I am new to wordpress and following is my scenario.
I am inserting an external php script in wordpress by creating a new template in the theme and then using that template in a new page.
When I do this the new content is visible in the loaded page (and works as expected) but the theme breaks for the page i.e. all side bars (right and bottom) get lost. and if i am logged in the wpadmin bar at top is lost for that page only.
for all other pages everything comes back.
Could you guys please help me what could be going wrong here.
I doubt that there is some folder config going wrong somewhere.
Following is what I am doing:
inside my new theme page template -
<?php
/**
* The template for displaying 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.
*
* Template Name: abctemplate
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
?>
<?php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
include_once dirname(ABSPATH) . '\abc\index.php'; // <=== the EXTERNAL SCRIPT
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
echo "end post loop";
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php echo "get_sidebar"; get_sidebar(); ?>
<?php echo "get_footer"; get_footer(); ?>
The above script internally after setting some variables, calls following template:
require_once('templates/'.$template.'/index.php');
the above template is a simple html page calling some variables in above abc/index.php
calling this breaks the wordpress theme mostly, the sidebars, (i am not sure yet if it breaks something else).
Could this mean that wordpress did not find the required side bar related files? but everything is inside the theme template.
Basically this whole thing is a scenario of loading an existing webpage into wordpress. I have the functionality working but UI breaks.
The problem I see first is naming the templates.
you have the following, which will throw a PHP parse error:
<?php
Template Name: abctemplate
?>
Have a look at the documentation here: https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/
A template name should be in a docblock as follows
<?php
/**
* Template Name: Full Width Page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
Guess what ! it was the die() which was breaking everything. And the way I found it was totally lame(removing approx 2000 lines of code one by one). Anyways the reason makes a bit sense to me now. Thanks!

Why do wordpress theme loops in a single page

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.

Some information about how to work the single.php WordPress file?

I am pretty new in WordPress theme development and I have some doubts about the single-php file that show a single post.
I have created this single.php file starting from my index.php file:
Testo Introduttivo
Introduction Text
<!-- SEZIONE IN CUI VENGONO VISUALIZZATI I POST DEL BLOG: -->
<section id="blog-posts">
<header class="header-sezione">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
// Previous/next post navigation.
//twentyfourteen_post_nav();
// 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;
?>
</header>
<!-- Qui viene visualizzato il singolo articolo -->
</section>
<section id="partnerSlide">
<header class="header-sezione">
<h2>Partner e Sostenitori</h2>
</header>
<div class="row">
<?php
// 'My_Widgtet_Area' area, where the id is called:
if (is_active_sidebar('partner-slide')) : ?>
<div id="widget-sidebar">
<ul>
<?php dynamic_sidebar('partner-slide'); ?>
</ul>
</div><!-- #widget-sidebar .widget-area -->
<?php endif; ?>
</div>
</section>
So I have take the index.php file and I deleted all the content that I do not want to be shown in the post visualization (So I have kept only the skeleton of my index.php file theme)
Then I have add this code to show my post:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
// Previous/next post navigation.
//twentyfourteen_post_nav();
// 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;
?>
It work correctly but I have some problem to understand what the previous code exactly do:
I know that by this line I am refering on The Loop to my clicked post (the post to show):
while ( have_posts() ) : the_post();
The thing that I can't understand is what exactly do when perform this operation:
get_template_part( 'content', get_post_format() );
Reading on the documentation (http://codex.wordpress.org/Function_Reference/get_template_part) it seems to me to understand that it load a predefinied template into my theme (like a code snippet putted into my theme)
I think that this put the code that print my post on the page (the title, under it the author name and the datem under it the post text and finally the category and the add comment link)
In practice is as am I putting the content.php file in the position where I have declared the previous code?
Tnx
Andrea
Is it true?
Andrea,
You are correct. This is the equivalent to an "include".
To break this down
get_template_part( 'content', get_post_format() );
get_template_part // is calling a function to locate the template partial aka include
'content' // this is the base slug.
// Think of it like a root word as opposed to a suffix.
get_post_format() // is saying get the particular content "partial" or "include"
What this is doing?
Let's say you're on a post with the format of "Video" (you know on the right hand side when you are creating a post, you can choose the "post format" --)
Standard
Audio
Aside
Chat
Gallery
Image
Link
Quote
Status
Video
Okay, lets assume you've clicked the radio button for "Video". So this post contains a video.
NOW, back to your get_template_part...
This will look FIRST for a file called "content-video.php"
If you have not created one, then it will fall back to the default file "content.php"
Thats why you have the second item in get_template_part.
Because it gives you a sort of "fall-back" in case a specific template it missing, it can then look for the "default" template.
The order it will look for the file is this
If you are using a child theme --
It will look first in your child theme folder / directory
content-video.php
content.php
Then if it cannot be located there, it will look for the files in the parent theme folder
content-video.php
content.php
You are using a child theme right?>?>
The additional beauty you can use it anywhere...
And even include it conditionally.
For example if you wanted to include an offer to signup for eNews but only if someone is reading a post in a specific category... you could do the following...
if (in_category( '327' )){
get_template_part('partials/enewssignup');
}
This would reference the file enewssignup.php located in the /partials folder.

Categories