Clone an entire page to custom index.php - php

I am building a custom index.php file in my child theme overriding the default theme index.php file which loads first when the theme is first installed.
Is there a way to clone an entire page to index.php file by page name or id or slug? I will build a page by adding a new page from dashboard and add elements there. Now, when I build the page, I also want the index.php file look the same. Hope I make sense.
I have created a custom template as the index.php file. But not sure about the cloning process. Any help will be appreciative.
thanks

You can get the code from page.php of your parent theme inside your index.php - in this file you'll have all that is needed to display a page.
For example in WP default theme, this code from page.php will display the current page using content-page.php template :
while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endif;
endwhile;
Then, in WP settings, define your page as home page in Settings > Reading - it will tell wordpress to use that page for your index.php template.

I got it resolved this way.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_format = et_pb_post_format(); ?>
<?php
$page = get_page_by_title('Default Homepage');
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>

Related

WordPress not using category.php file for custom theme

I'm coding a WordPress theme on a staging site and I'm having an issue with having all of my posts display. I've set the static page for the blog to "News" in the setting > Reading option but when I go to the news page, the page is displaying using the index.php file instead of the category.php file that I have created. Template order should have index last if it can't find category or archive is my understanding.
I'm using the same coding on another site and it works correctly. I've put this new theme on that existing site and it works as it should. They are both on the same hosting company. I started a brand new WordPress instance and it's not working on that new instance either.
My template for page.php is working fine instead of defaulting to index.php
Also tried updating permalinks, checked file permissions, checked htaccess file for rewrite conditions.
So what am I missing?
category.php code
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content-posts', get_post_format() ); ?>
<?php endwhile; else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
<?php echo paginate_links(); ?>

How is get_header() calling a specific PHP file?

I'm trying to learn Wordpress Structure with investigating some free plugins/themes.
I'm working on "Renger Blog Theme" right now, but I couldn't understand something. I've checked get_header() page in the WP manual but it still looks like a magic :)
This theme has custom function codes in
wordpress\wp-content\themes\renderblog\inc\renderoption.php
and this theme calling this file with just
get_header();
in index.php
There is no include code in header.php or wherever else.
How it's calling this specific PHP file with get_header()? Is it like a way to include automatically all files in the inc folder?
When I just delete get_header() from the index.php, the functions are not working.
WordPress get_header() is the function predefined by WordPress inbuilt structure. This function Includes the header template for a theme or if a name is specified then a specialized header will be included.
if the file is called "header-new.php" then specify "new".
For example <?php get_header('new'); ?>
Another example for Different headers for different pages.
<?php
// if detect home page of wordpress
if ( is_home() ) :
get_header( 'home' );
// if detect Not found page 404 of wordpress
elseif ( is_404() ) :
get_header( '404' );
// default header if nothing specific is detected
else :
get_header();
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.

Show posts in theme development

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.

Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)

Categories