Show posts in theme development - php

I am new in Wordpress. I am developing my own theme. I have included index.php, functions.php, header.php, footer.php, page.php, style.css etc.. Now my home page is working perfectly. When I am going to display category menu its showing only header and footer. Not inside the content. When displaying post also its showing Header and footer.
What are the steps included for displaying posts in theme development or is there any function I want to include in function.php ?

You need to use "the loop" to display the content from the page/post.
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<header><h1><?php the_title();?></h1></header>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Copy this code into a new file called single.php in your theme folder.

Best way is to copy the existing theme and change the theme name ( make changes in css , images , header , footer design ) and use that , it will work correctly.

If you are working with child theme.
than all pages display under default template. and default template is page.php.
for category there is a category.php or archive.php file in which you have to change code for more functions.
And for display post you have to change single.php
you can create template for additional functionality.
that's it.

You might be missing content.php, category.php,content-single.php and many more files which in result not displaying your content part and only displaying header and footer.
Best way is download fresh word press from :
https://wordpress.org/download/
It will include all supporting files for load website.copy one of theme from fresh themes folder and replace it with your theme and customise in it.

Related

How do I include the blog page in a custom WordPress theme?

I am pretty new to WordPress and I am trying to make a basic custom theme from scratch. The basic navigation menu is displayed and works on all of my pages except for the default blog page (which just appears as a blank white screen when clicked on). I have my theme split up into multiple files (header, footer, page, archives, etc). I think that the page.php file is what is supposed to display the blog, but it only works on the home, about, and test pages.
page.php:
<?php get header();?>
<div>
<h1><?php the_title();?></h1>
<?php if (have_posts()) : while(have_posts()) : the_post();?>
<?php the_content();?>
<?php endwhile; endif;?>
</div>
<?php get_footer();?>
post.php is not for the post type post it is for static pages or custom post-types.
You are looking for single.php or index.php.
This template Hierarchy from the Wordpress docs will help you.
Also see: https://developer.wordpress.org/themes/basics/template-hierarchy/
Style.css
Go to the WordPress default theme folder, open the style.css file. Copy the commented code at the top and paste it to the GlossyBlue style.css file. Change the theme name and the author information as you desire.
Splitting The Files
Now you need to understand where to split the file into several files: header.php, sidebar.php, and footer.php. The image below shows a simplified version of my index file and how the markups should split.
Header.php
Open the index.html file. Cut from the top to where the ends, paste it in a new PHP file, and save the file as header.php.
Sidebar.php
Back to the index.html file, cut from where the start to the closing tag of and paste it in a new PHP file, save it as sidebar.php.
Footer.php
Back to the index.html file, cut from the tag to the end of and paste it in a new PHP file, save it as footer.php.
Index.php
Now in your index.html file, you should only have the wrap. Save the file as index.php. Insert the line:get_header, get_sidebar, and get_footer in the same order as your layout structure.
Single.php
Now, it is time to do the single.php template. If you want, you can go through the same process — cut & paste from the default theme. But, I find it easier to use the index.php that you just created and save it as single.php. Open the default theme single.php file and copy the Template Tags over. Then include the comments_template. The image below highlights what I’ve changed:
Page.php
With the single.php template you just created, save it as page.php. Remove the post date, comment form, next/previous link… and that’s it.. there goes your page.php template.
Detailed document here : http://webdesignerwall.com/tutorials/building-custom-wordpress-theme
You question answer :
Copy page.php and rename it to single.php file.

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;
?>

Wordpress: prevent code dupplication through similar page templates

I'm currently doing some refactoring on an existing wordpress website that uses 9 page templates and I have the following problem:
All these pages have the same code written to display the header, the sidebar etc.
I wonder if it's possible for me to have a "parent" page that would be in charge of displaying those recurring template parts?
The reason I can't manage to do it is that only the page template file is called and no parent file.
That's what I would like to achieve:
<?php
// page-master.php
get_header();
get_sidebar();
get_page_content(); // includes the actual page template.
get_footer();
?>
I hope you understand what I'm looking for :)
Thanks in advance!
Create template your-name.php
Put that code
<?php
/*
Template Name: Your Name
/*
get_header();
get_sidebar();
get_template_part(template-hepler/your-name-content.php); // includes the actual page template.
get_footer();
?>
Create your-name-content.php in template-helper folder
And write there your page content
Assing that template to nessasary pages

Convert html to wordpress & provide link to menu inside panel

I have an HTML template which I am trying to convert into PHP for WordPress.
The homepage has been converted and is shown properly. Next, the menu in navbar is about, the page that needs to be converted in PHP.
I have just added get_header() at the top and get_footer() below. In between these two I have added the HTML content for my entire page.
Then I tried to provide the link for the menu that I created, but the content of about page is not visible.
Do I need to add any other line for that page apart from get_header and get_footer? Or is something wrong with the link?
Clearly I can't understand what your are trying to say. I think you want to create a page that will show your about menu content.
So in your theme directory create a page name page.php and use get_header(); in first and below get_footer and where you want to show page content write the_content();. like this
<?php
get_header(); ?>
<div><?php the_content(); ?></div>
<?php get_footer(); ?>
after that follow the url of # Sajid Anwar. because for dynamic data you have to create pages(home,about etc) from wordpress dashboard and in menu section add these pages as navigation menu to menu.

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.

Categories