That’s not how WordPress works. You might want to read some of the developer docs at https://developer.wordpress.org/ and understand the template hierarchy.
This is too complicated. If you can’t make templates and reuse them at will, why would you want to use this over plain PHP?
Anyhow, if anyone can think of a simple, normal way to reuse code on multiple pages in WP, all ideas are welcome.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
Joy
(@joyously)
You can do that with my theme. Each template part (in my theme and child theme) is presented as a widget, which you can drag and drop wherever, since the whole theme is 4 widget areas.
https://wordpress.org/themes/twenty8teen
I was told recently that my theme is not the only one to do this.
Edit: Also, the block editor has a thing called reusable blocks. Also, they are working on templates that you can save in the database to put wherever you want.
-
This reply was modified 6 years, 3 months ago by
Joy.
You’re missing the point. A theme is a set of templates that builds pages.
What is it, stepping back from code, that you want to do?
Templates that build pages is what I need, but I can’t think that out of that graph posted.
If there is a section in a book that refers to exactly this item, I’d be thankful for a link.
As I understand from the link you posted….
Single Page #Single Page
The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following path:
custom template file – The page template assigned to the page. See get_page_templates().
page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.
page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.
page.php
singular.php
index.php
… I can have page templates in my code, I just have to name the files in a right way:
page-main.php
page-about.php
page-contact.php
WP should automatically add the content defined in these files to pages named main, about, contact, respectively?
But how do I make a:
section-header.php
section-sidebar.php
section-footer.php
That I can include (or skip) in whichever page I like? And reuse code! That’s the main thing I’m after.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
I’m not going to argue it with you… the freedom of WordPress is you can do it however you like.
But to learn how it’s actually done, here’s a good tutorial: https://github.com/Automattic/_s
Joy
(@joyously)
I’m not here to argue. I’m asking HOW to do it, if it’s even possible. What to do? To reuse code from one PHP file on multiple pages in WP.
These are just WP functions listed which is pretty much useless. One can’t make much of text like this:
https://developer.wordpress.org/reference/hooks/get_template_part_slug/
I’m sorry if I’m stupid, but I don’t quite understand what hook is, what is hook name, what are portions of it’s name, and which portion is dynamic…
Can’t you just point the function that adds code into page? Like:
wp_inserts_your_code_here (“file_path“);
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
Joy
(@joyously)
It seems that you ignored the easy function on that page: get_template_part()
Each function has a page dedicated to explaining the function and its parameters. Click on the links.
Joy, thank you. Finally you got me a token with which I found it on YT how it’s done, and it is indeed very simple.
I missed it cause I don’t understand a single thing from text.
Don’t know what template is in context of WP environment. What’s the difference between template and template part. Can’t find what hook is, what is firing, who fires it, on what trigger. Don’t quite understand what slug is, what’s the use of it, why there was a need for it to be different from file, etc…
Maybe for you people who have actually SEEN all these things the text on developer part of the site, most of it actually make sense, but for the rest of us it’s like:
get_template_part
Parameter a of entity x: Does action α before entity y is loaded.
get_template_part()
Function: Loads an entity y into entity z.
Anyways, I got what I wanted. Again, thank you very much Joy.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.
-
This reply was modified 6 years, 3 months ago by
kornelije.