WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using A Custom Page Template Based On PHP Switch Statement (10 posts)

  1. kirkward
    Member
    Posted 1 year ago #

    Hi,

    I am creating a Multsite Network for a particular group of users. The site includes a category hidden from the Site Admins in the WP dashboard. They cannot edit posts in this category or add new posts to this category. Only the Super Admin can do that.

    Because I am using a custom post template for this category, the list of custom post templates is available to the Site Admin on the Edit/Add Post page. My best solution (so far) has been to have several templates created in another folder and use the PHP Switch statement to select the correct template for the post using a variable I have stored in the database.

    Here is the code from my custom template:

    <?php
    /*
    Template Name Posts: Niche Posts
    */
    ?>
    <?php $niche = 'cow'; 
    
    switch ($niche) {
        case "store":
            include "http://mysite.com/wp-content/uploads/stores/store-template.php" ;
            break;
        case "bar":
            echo "i is bar";
            break;
        default:
    	include "http://mysite.com/wp-content/themes/mytheme/template-fullwidth.php") ;
            break;
    }
    ?>

    I am returned a blank page.

    The templates work properly when called directly, but not when I try to include them.

    I have also tried using the ABSPATH function, with similar results.

    <?php include(ABSPATH . "../template-fullwidth.php") ; ?>

    Any help someone can give would be appreciated.

  2. wpismypuppet
    Member
    Posted 1 year ago #

    Try using WordPress' function get_template_part(). Though it sounds weird cause you aren't really getting a "part" of a template, it is indeed what you want. So you would call:

    switch ($niche) {
        case "store":
            get_template_part( 'store', 'template' );
            break;
        case "bar":
            echo "i is bar";
            break;
        default:
    	get_template_part( 'template' , 'fullwidth' );
            break;
    }

    Read up on the documentation to see why I pass what I pass, but this should work. The function basically does the include() function for you, but it knows the right path, and it checks to make sure the file actually exists and does a graceful fail instead of PHP errors.

  3. kirkward
    Member
    Posted 1 year ago #

    I had never heard of get_template_part before today (and as a non-coder, I couldn't understand it).

    Taking the line

    get_template_part( 'store', 'template' );

    does that search until it finds the "store-template" in any directory or folder in wp-content?

    My site admins will be able to change themes, and I do not want to have to copy all the templates into each and every theme, only the main one.

    Edit: I forgot to mention that the function works great for the template that is in the theme folder. Can you give any guidance for the folder parallel to the theme folder (and maybe not on the same level)?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    does that search until it finds the "store-template" in any directory or folder in wp-content?

    No. It will only look in your theme's top level folder.

  5. wpismypuppet
    Member
    Posted 1 year ago #

    Well, if each of your themes are child themes off the main one, this would work fine. The function will check the current child theme first, then check it's parent. It does not search the entire wp-content folder.

    If you need that functionality, then you'll want to use the absolute path. However the WordPress ABSPATH variable changes with each page. And adding that with a ../ will cause problems.

    To get the absolute path of your website, you'll want to use the PHP variable $_SERVER['DOCUMENT_ROOT'] and then add in the rest. So something like this:

    <?php include($_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/stores/store-template.php") ; ?>

  6. kirkward
    Member
    Posted 1 year ago #

    How can I include and use templates that are not in the theme folder?

    I tried the full path in the get-template_part function, alas, to no avail. i.e.

    get_template_part('full_url_to_template');

    didn't work.

  7. kirkward
    Member
    Posted 1 year ago #

    Sorry esmi and wpismypuppet. Seems it takes me longer to think and try than it does for you guys to answer.

    I'll be back in a moment.

  8. kirkward
    Member
    Posted 1 year ago #

    That Is So Cool!

    Thanks.

    <?php include($_SERVER['DOCUMENT_ROOT'] . "/wp-content/uploads/stores/store-template.php") ; ?>

    worked for my store templates and

    get_template_part('single') ;

    keeps the managers out of trouble!

    Thanks, thanks, thanks.

  9. wpismypuppet
    Member
    Posted 1 year ago #

    You are welcome... glad to be of help.

  10. kirkward
    Member
    Posted 1 year ago #

    I know this thread is marked resolved, and it is. I didn't know where else to make this comment.

    I'll be 71 y.o. next month, and I started with WordPress because I couldn't understand the coding folks were using to build my websites. As time goes on, and I want to do more and more, I keep getting amazed at how powerful WordPress is, and how it can be tweaked with small snippets of code. A person doesn't have to know a whole language (while I'm sure it does help) in order to accomplish a lot with WP. Keep after the core coders to keep innovating. It's really cool to watch this thing develop.

    Kirk

Topic Closed

This topic has been closed to new replies.

About this Topic