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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助