• I’d lake to make section files, something like:

    header.php

    section-A.php
    section-B.php
    section-C.php
    section-D.php
    section-E.php

    sidebar-A.php
    sidebar-B.php
    sidebar-C.php
    sidebar-D.php

    footer-1.php
    footer-2.php

    And then just arrange them like puzzles on different pages (pseudocode):

    page1:
    use( header.php);
    use( section-A.php);
    use( section-B.php);
    use( sidebar-A.php);
    use( footer-1.php);

    page2:
    use( header.php);
    use( section-A.php);
    use( section-C.php);
    use( sidebar-B.php);
    use( footer-2.php);

    etc.. You get what I mean. I was under the impression this can be done very simple in WP, but I can’t find out how.

    • This topic was modified 6 years, 3 months ago by kornelije.
    • This topic was modified 6 years, 3 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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.

    Thread Starter kornelije

    (@kornelije)

    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.

    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.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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?

    Thread Starter kornelije

    (@kornelije)

    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.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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

    Thread Starter kornelije

    (@kornelije)

    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.

    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.

    Thread Starter kornelije

    (@kornelije)

    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.
Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Reusable Code’ is closed to new replies.