0

I'm trying to assign a php file to a specfic page? I have modals stored in different files and want have them load in when their assigned page is called. These pages are using the same theme, "Portfolio".

If I do something like:

for each file in my Portfolio template, each page will still load all of the modals that are assigned, not just the one that I'm try to assign to that page. Is there a way to try this in functions.php? EDIT: My apologies for not being clear, so I'll show the pages. Here's a sample of the portfolio page with the image buttons: [sample portfolio page][1]. Apologies if you can't view the link, it's still on the server and hasn't launched just yet. All of the "work" pages are going to hold the modal buttons. All of these pages will be duplicates of the same page template. So that load won't be affected by having a bunch of modals just hanging in my header, I think it would be great to have the modals properly cataloged and only called when needed, sort of like a data feed (I imagine). In my theme, there's a folder called *"modals"* and for this page we would need to call '/modals/work/thunderModals.php' for 'page-id-1270'. I have not yet implemented the last 2 suggestions but I will get started. Thank you to everyone who is helping out on this. Your time is greatly appreciated. [1]: http://rpm2018.rpmadv.com/index.php/work/thundervalley/ So, I read through the wp hierarchy and through there I was looking for how to properly target slugs and I found this: ID ); ?>

I went back to my portfolio.php template (which is the shell for all of my work/child pages). So to that I appended an 'include'. Now I have something like this:

<?php get_page_template_slug('page-id-1270'); include '/modals/work/thunderModals.php';?>

Seems to work so I added a few more lines for my other pages. But when I got to my other portfolio.php-themed pages, I can see that all of the modals are loaded in, including the modals for the other pages. New question:

Can this line of code be modified so that the files will remain specific to their page?

  • Are you aware of the template hierarchy? It provides a straight forward mechanism for page loading in various situations that would be very useful for your purposes. Can you link to the theme you used though? It's difficult to find it given such a generic name, a search of .org for portfolio gives 537 results – Tom J Nowell May 18 '18 at 1:48
0

Use the template hierarchy, you can name files in your theme so that they load in specific circumstances.

E.g. the template category-unicorns.php will be used instead when on the category with the slug unicorns

0

If I understood you right, a good solutions is to check which page you are in, using the page slug, and then include the file you need. You could do this on your theme's functions.php or on a plugin, depending on what you want to achieve. To do that, you can use the following script:

if (is_page('slug_of_the_page')) {
    include 'path-to-the-file';
}

Now, if you really want to target the page by its ID, you can create a function that uses an action hook from the page template you are using, or even add your own action hook there. Then you get the current page id with $wp_query and use a switch statement to check if the id is the one you want and include the file. See the following script:

function add_specific_page_file() {
    global $wp_query;
    switch ($wp_query->post->ID) {
        case 'page_id':
            include 'path-to-the-file';
            break;

        case 'another_page_id':
            include 'path-to-another-file';
            break;

        case 'one_more_page_id':
            include 'path-to-one-more-file';
            break;
     }
}
add_action( 'my_action', 'add_specific_page_file' );

[EDIT] You can integrate the is_page with the previous function and use the init action hook, which fires after WordPress has finished loading but before any headers are sent.

function add_specific_page_file() {
    if (is_page('slug_of_the_page')) {
        include 'path-to-the-file';
    } elseif (is_page('another_page_slug')) {
        include 'path-to-another-file';
    }
} 
add_action( 'init', 'add_specific_page_file' );
  • Didn't work Filipe, but I also didn't get an error message. Would there be a way to incorporate the is_page() parameter? – David Sanchez May 17 '18 at 19:22
  • @DavidSanchez Could you show me your code, please? Edit it into your question. Which action did you use from your page template? You can either create one using do_action('my_action'); and place wherever you want it, or you could use the ini, which will load in the header. And yes, you can use the is_page in this last function. Have a look on my last edit in my previous answer and try to use that function. You only need to change the pages slugs and file paths there. – Filcp May 17 '18 at 23:35
  • @FilipeCarranoPacheco it's init not ini, eitherway that will still load the template originally intended and you'll end up with 2 fullpage templates loaded at once – Tom J Nowell May 18 '18 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.