dsw7547 2015-07-13 16:00
浏览 40
已采纳

单一自定义Wordpress儿童页面

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'; ?> 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'; ?> 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!

  • 写回答

1条回答 默认 最新

  • dousha7645 2015-07-13 16:54
    关注

    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(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程