Custom WordPress template loads only header & footer - php

I have a weird problem. I'm trying to make custom template for WordPress.
index.php works perfectly fine - everything what should load, loads with no problems.
index.php:
<?php get_header(); ?>
<?php if ( is_home() &&function_exists('get_template_part')) get_template_part( 'content', get_post_format() );?>
<?php get_footer(); ?>
But when I create a new page, like promotions.php and put custom code, It does not show up on the page. I can see only header & footer section. It looks like I didn't put any code at all.

You can make custom page templates like so
<?php
/**
* Template name: custom-name
*/
get_header();
if (is_home()) {
get_template_part('content', get_post_format());
}
get_footer(); ?>
And then inside WordPress you can edit the page. On the right side below publish you can see default template. Choose your template and hit save.
Edit:
See the image below for further instructions.

Use flush_rewrite_rules() function one time in functions.php.
All should works after that.

Related

Need Help Hiding Sidebar on Custom Wordpress Php file

I am using the underscores theme to build my wordpress site. It is very barebones (which I like), but they didn't seem to include a page.php and post.php file, instead they just have page. This means if I have a sidebar, it shows on all pages and posts. The code to show the sidebar is
<?php if ( ! is_active_sidebar( 'sidebar-1' )) {
return;
}
?>
I've tried a number of if statements to exclude pages but I can't seem to get it right. How would I have it so if the person is on a page it doesn't return the sidebar?
Thanks!
Edit the page.php and REMOVE the sidebar:
<?php dynamic_sidebar( 'sidebar-1' ); ?>
or the get_sidebar() function.
get_sidebar();

WordPress custom template not displaying custom HTML, Custom fields, etc

I am developing a custom theme in WordPress, and am trying to put custom HTML and PHP in my home page template. Whenever I am posting code for divs or custom fields in my home page template, they are simply not rendering and displaying on the front end. In the example below, the <div class="col-full"> is not being shown on the front end.
<?php
/*
Template Name: home
*/
?>
<!DOCTYPE html>
<?php get_header(); ?>
<center>
<div class="col-full">
<?php get_template_part( 'content', get_post_format() ); ?>
</center>
<?php get_footer(); ?>
Looking at your code, you've set it up as a template file. These need to be manually selected from the page as the template for it to work.
I'd recommend consulting the wordpress template hierarchy https://wphierarchy.com/. This shows you which files will try to be loaded based on what page you're visiting.
If you're still having trouble, i'd suggest installing the plugin https://en-au.wordpress.org/plugins/query-monitor/, which will tell you the template being loaded as well as other handy information.

Adding content to wordpress page

So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>

Category page in wordpress

I am developing a custom wordpress theme ( first time) , I have created below files and they work fine by following this tutorial.
index.php
header.php
footer.php
single.php
sidebar.php
Now issue is when I click on a category name at home page it show me 404 error, I tried to create category.php but still same issue. here is my index.php which I used for category.php
<?php get_header(); ?>
// other html stuff and loop
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile;?>
<?php endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I have two question:
Is it really require to create category.php or can we use index.php for same purpose to show posts from category or tags?
If it's require to create category.php, Do I also need to create tags.php and how will I get posts for those?
No, it is not required to have category.php file in a WordPress theme. You can see following link for finding out template hierarchy in WordPress works.
https://developer.wordpress.org/themes/basics/template-hierarchy/#category
You can see that, if there is no category.php file in theme, it ultimately falls back to index.php.

Displaying varying Wordpress content using get_template_part

I'm working on a Wordpress landing page template and the way I want it to work is that each content-xxxxxx contains a different block of text/images etc. Then on the Front Page I can call them like;
<?php get_header(); ?>
<div class="container">
<?php get_template_part( 'content', 'featuredcontent'); ?>
<?php get_template_part( 'content', 'textblock'); ?>
</div>
<?php get_footer(); ?>
Which is great and works fine, but I want to be able to choose the order of the blocks from within WordPress without having to edit the front-page.php file, does anyone have any idea or code that can help with this?
Thanks

Categories