Wordpress二十一主题中的自定义页面模板不显示为页面部分?

时间:2018-01-09 16:42:46

标签: php wordpress

我创建了一个页面模板并将其作为页面的主题。之后我选择了此页面作为二十七个主题选项中的页面部分,但是如果您直接访问该页面而不是页面部分,则不会显示此页面的内容。

<?php
/*
Template Name: Contact
*/
?>

<?php
if(isset($_POST['submitted'])) {
    if(trim($_POST['contactName']) === '') {
        $nameError = 'Please enter your name.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    if(trim($_POST['email']) === '')  {
        $emailError = 'Please enter your email address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    if(trim($_POST['comments']) === '') {
        $commentError = 'Please enter a message.';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    if(!isset($hasError)) {
        $emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = get_option('admin_email');
        }
        $subject = '[PHP Snippets] From '.$name;
        $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }

} ?>
<?php get_header(); ?>
    <div id="container">
        <div id="content">

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                <h1 class="entry-title"><?php the_title(); ?></h1>
                    <div class="entry-content">
                        <?php if(isset($emailSent) && $emailSent == true) { ?>
                            <div class="thanks">
                                <p>Thanks, your email was sent successfully.</p>
                            </div>
                        <?php } else { ?>
                            <?php the_content(); ?>
                            <?php if(isset($hasError) || isset($captchaError)) { ?>
                                <p class="error">Sorry, an error occured.<p>
                            <?php } ?>

                        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
                            <ul class="contactform">
                            <li>
                                <label for="contactName">Name:</label>
                                <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
                                <?php if($nameError != '') { ?>
                                    <span class="error"><?=$nameError;?></span>
                                <?php } ?>
                            </li>

                            <li>
                                <label for="email">Email</label>
                                <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
                                <?php if($emailError != '') { ?>
                                    <span class="error"><?=$emailError;?></span>
                                <?php } ?>
                            </li>

                            <li><label for="commentsText">Message:</label>
                                <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                                <?php if($commentError != '') { ?>
                                    <span class="error"><?=$commentError;?></span>
                                <?php } ?>
                            </li>

                            <li>
                                <input type="submit">Send email</input>
                            </li>
                        </ul>
                        <input type="hidden" name="submitted" id="submitted" value="true" />
                    </form>
                <?php } ?>
                </div><!-- .entry-content -->
            </div><!-- .post -->

                <?php endwhile; endif; ?>
        </div><!-- #content -->
    </div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

我希望你能帮助我们。提前致谢! :)

修改

直接访问页面时会显示包含自定义页面模板的网站,例如http://example.com/testing

direct link

但是如果你想在主题选项下的二十七个主题中将其设置为页面部分,那么自定义模板应该出现的空间是空白的。如果您通过http://example.com访问它,就属于这种情况。该网站是onepager,带有自定义页面模板的测试页面是其中的一部分。

front page

第二次编辑:

我将自定义页面设置为页面部分,只需在主题选项中进行设置即可。

theme options of twentyseventeen theme

1 个答案:

答案 0 :(得分:0)

您正在尝试使用wordpress中的wordpress函数。将此代码require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');直接放在代码第8行的开始php代码<?php的下一行。

如果您的wordpress安装位于主服务器目录的某个文件夹中,则此功能可能无效。然后你可以尝试这个:

//place this at the same position, as I mentioned above
$needPath = realpath(__DIR__ . '/../../..');
require_once($needPath . '/wp-load.php');

来自here的已使用的解决方案在文件系统中提升了几个级别。因为我们需要从{wp-main-folder}/wp-content/themes/twentyseventeen/{your-file}转到主要的wordpress安装文件夹,所以我们应该向上移动3级以获取wp-load.php文件。

修改

此外,如果您的WordPress安装调用了您的页面,将会很有用。因此,您可以使用此解决方案:

//place this to the same place as described above
if(!defined(ABSPATH)) {
    $needPath = realpath(__DIR__ . '/../../..');
    require_once($needPath . '/wp-load.php');
}

经过测试和工作

修改:更详细

<?php
/*
Template Name: Contact
*/
if(!defined(ABSPATH)) {
    $needPath = realpath(__DIR__ . '/../../..');
    require_once($needPath . '/wp-load.php');
}

if(isset($_POST['submitted'])) {
    if(trim($_POST['contactName']) === '') {
        $nameError = 'Please enter your name.';
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    if(trim($_POST['email']) === '')  {
        $emailError = 'Please enter your email address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    if(trim($_POST['comments']) === '') {
        $commentError = 'Please enter a message.';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    if(!isset($hasError)) {
        $emailTo = get_option('tz_email');
        if (!isset($emailTo) || ($emailTo == '') ){
            $emailTo = get_option('admin_email');
        }
        $subject = '[PHP Snippets] From '.$name;
        $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        wp_mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }

} ?>
<?php get_header(); ?>
    <div id="container">
        <div id="content">

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                    <div class="entry-content">
                        <?php if(isset($emailSent) && $emailSent == true) { ?>
                            <div class="thanks">
                                <p>Thanks, your email was sent successfully.</p>
                            </div>
                        <?php } else { ?>
                            <?php the_content(); ?>
                            <?php if(isset($hasError) || isset($captchaError)) { ?>
                                <p class="error">Sorry, an error occured.<p>
                            <?php } ?>

                            <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
                                <ul class="contactform">
                                    <li>
                                        <label for="contactName">Name:</label>
                                        <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
                                        <?php if($nameError != '') { ?>
                                            <span class="error"><?=$nameError;?></span>
                                        <?php } ?>
                                    </li>

                                    <li>
                                        <label for="email">Email</label>
                                        <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
                                        <?php if($emailError != '') { ?>
                                            <span class="error"><?=$emailError;?></span>
                                        <?php } ?>
                                    </li>

                                    <li><label for="commentsText">Message:</label>
                                        <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                                        <?php if($commentError != '') { ?>
                                            <span class="error"><?=$commentError;?></span>
                                        <?php } ?>
                                    </li>

                                    <li>
                                        <input type="submit">Send email</input>
                                    </li>
                                </ul>
                                <input type="hidden" name="submitted" id="submitted" value="true" />
                            </form>
                        <?php } ?>
                    </div><!-- .entry-content -->
                </div><!-- .post -->

            <?php endwhile; endif; ?>
        </div><!-- #content -->
    </div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

wordpress调用此模板。您需要将其称为(如果您的网站网址为http://example.com):

从wordpress page创建dashboard,然后从template右侧的列表create page section中选择,而不是要使用的Default Template

how it looks

之后(不要忘记添加page title)保存页面并转到url,在那里提供。如果网页没有parent,或永久链接为?p=123,那么您应该像以下网址一样访问该网页:http://example.com/your-page-title

如果您要直接访问该模板,则应将url写为http://example.com/wp-content/themes/twentyseventeen/your-template-filename.php。此示例作为模板提供,直接位于twentyseventeen theme文件夹中。