简体   繁体   中英

Single Custom Wordpress Child Page

I have 5+ blogs that I manage and I'm trying to make the page layout a little more efficient for updates. For each blog, they have about 5-6 custom pages with generally the same code, except for the content area. This is an example of what I've written successfully already.

( sorry for the length, trying to be thorough on the background here )

<?php 

/* Template Name: Contributors Page */

get_header(); ?>

<div id="main-content">
    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">
                <?php include 'content-contributors.php'; ?>
            </div> <!-- #left-area -->

            <?php get_sidebar(); ?>
        </div> <!-- #content-area -->
    </div> <!-- .container -->
</div> <!-- #main-content -->

<?php get_footer(); ?>

This code would be standard on all custom pages except for the include link.

For example, I would use the line <?php include 'content-contributors.php'; ?> <?php include 'content-contributors.php'; ?> to call the following file:

<?php 

// Get all users, display in alphabetical order
$allUsers = get_users('role=contributor&orderby=display_name&order=ASC&exclude=');

$users = array();

// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
    if(!in_array( 'subscriber', $currentUser->roles ))
    {
        $users[] = $currentUser;
    }
}

?>

<section class="author-content" role="main">
<!-- insert content here -->
</section>

Since this would be very repetitive when updating upwards of 20-30 some pages, I wanted to shrink this down to one custom page per blog. Here's what I tried:

<?php 

/* Template Name: Custom Page */

get_header(); ?>

<div id="main-content">
    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">
                <?php include get_stylesheet_directory_uri() . '/includes/inc-page-content.php'; ?>
            </div> <!-- #left-area -->

            <?php get_sidebar(); ?>
        </div> <!-- #content-area -->
    </div> <!-- .container -->
</div> <!-- #main-content -->

<?php get_footer(); ?>

Here I've replaced the original include with <?php include get_stylesheet_directory_uri() . '/includes/inc-page-content.php'; ?> <?php include get_stylesheet_directory_uri() . '/includes/inc-page-content.php'; ?> <?php include get_stylesheet_directory_uri() . '/includes/inc-page-content.php'; ?> and calls this file:

<?php
    if ( is_page( 'archives' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-archives.php'";
    }
    elseif ( is_page( 'contributors' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-contributors.php'";
    }
    elseif ( is_page( 'subscribe' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-subscribe.php'";
    }
    elseif ( is_page( 'sitemap' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-sitemap.php'";
    }
    elseif ( is_page( 'tags' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-tag_cloud.php'";
    }
    elseif ( is_page( 'thankyou' ) ) {
        echo "include get_stylesheet_directory_uri() . "'/content-thankyou.php'";
    }

    else {
        // do nothing
    }

?> 

Unfortunately nothing happens. I've looked over my code and thought it made sense. Hopefully this is a simple fix. Maybe I'm over-complicating things.

Appreciate any help. Thanks!

get_stylesheet_directory_uri() returns the stylesheet URI ( URL ) - you can't include a php-file through a URL (actually you can in some cases, but you very rarely should ).

You should use get_stylesheet_directory() instead.

Also, as @Clyff pointed out, don't wrap include statements in echo statements - what you are doing is turning them into strings (and what's more your syntax in those lines is way off, your quotes don't add up, so I'm surprised if you don't get any fatal errors...?)

To include a file from the theme root do this:

include get_stylesheet_directory() . '/content-thankyou.php';

...or if you are already in another file in the theme root, this should do:

include 'content-thankyou.php';

BTW I don't really see any reason to include a file whose only purpose is to include other files. If I understand what you are trying to do, you can just put the whole thing in your custom template:

<?php 

/* Template Name: Contributors Page */

get_header(); ?>

<div id="main-content">
    <div class="container">
        <div id="content-area" class="clearfix">
            <div id="left-area">
                <?php
                if ( is_page( 'archives' ) ) {
                    include 'content-archives.php';
                } elseif ( is_page( 'contributors' ) ) {
                    include 'content-contributors.php';
                } elseif ( is_page( 'subscribe' ) ) {
                    include 'content-subscribe.php';
                } elseif ( is_page( 'sitemap' ) ) {
                    include 'content-sitemap.php';
                } elseif ( is_page( 'tags' ) ) {
                    include 'content-tag_cloud.php';
                } elseif ( is_page( 'thankyou' ) ) {
                    include 'content-thankyou.php';
                } else {
                    // do nothing
                }
                ?>
            </div> <!-- #left-area -->

            <?php get_sidebar(); ?>
        </div> <!-- #content-area -->
    </div> <!-- .container -->
</div> <!-- #main-content -->

<?php get_footer(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM