链接新的php页面wordpress


Link new php page wordpress

我正试图在index.php主题中使用引导选项卡来显示主页主题中的不同内容。我已经在index.php中实现了选项卡,并在选项卡popular中创建了一个名为popular-post.php的新页面。

但当我点击链接显示流行内容时,我会得到

致命错误:调用中未定义的函数get_header()

这是我的index.php 的代码

    <?php get_header(); ?>
    <div class="row" id="content">
        <div class="col-sm-8 col-md-8 col-lg-8" id="primary">
        <ul class="nav nav-tabs">
  <li class="active"><a href="#">Home</a></li>
  <li><a href="<?php bloginfo('template_directory'); ?>/popular-post.php">Popular</a></li>
  <li><a href="#">Recientes</a></li>
</ul>

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php
                /* Include the Post-Format-specific template for the content.
                * If you want to overload this in a child theme then include a file
                * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                */
                get_template_part( 'content', get_post_format() );
            ?>
        <?php endwhile;?> 
            <?php /* Pagnavi plugin support */ wp_pagenavi(); ?>
        <?php else: ?>  
            <?php get_template_part( 'no-results', 'index' ); ?>
        <?php endif; ?>
        </div>
        <div class="col-sm-4 col-md-4 col-lg-4" id="secondary">
        <?php get_sidebar(); ?> 
        </div>
    </div><!--/content-->
        <?php get_footer(); ?>

这是popular-post.php 的代码

<?php
/*
Template Name: Popular Posts
*/
?>
<?php get_header(); ?>
    <div class="row" id="content">
        <div class="col-sm-8 col-md-8 col-lg-8" id="primary">
        <ul class="nav nav-tabs">
  <li><a href="<?php bloginfo('template_directory'); ?>">Home</a></li>
  <li class="active"><a href="#">Popular</a></li>
  <li><a href="#">Recientes</a></li>
</ul>
<ul class="popular_posts">
        <?php $pc = new WP_Query('orderby=comment_count&#038;posts_per_page=10'); 
        while ($pc->have_posts()) : $pc->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <p>Posted by <strong><?php the_author() ?></strong> with <?php comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></p></li>
        <?php endwhile; ?>
    </ul>
        </div>
        <div class="col-sm-4 col-md-4 col-lg-4" id="secondary">
        <?php get_sidebar(); ?> 
        </div>
    </div><!--/content-->
        <?php get_footer(); ?>

提前感谢

您不能像<a href="<?php bloginfo('template_directory'); ?>/popular-post.php">Popular</a>那样直接链接到主题文件
这是一个无效的URL:http://example.com/wp-content/themes/YOUR-THEME/any-theme-file.php

创建一个新页面,"热门帖子",选择模板(你的文件已经有一个页面模板标题。注意页面ID(在URL中)。链接类似:

<a href="<?php echo get_permalink( THE-ID-OF-YOUR-PAGE ); ?>">Popular</a>

这将生成一个有效的URL:http://example.com/popular-posts/,它是您的一部分内容(页面),使用定义的页面模板文件。

要通过标题获取页面ID,请使用get_page_by_title:

$the_page = get_page_by_title('popular-posts');
echo '<a href="' . get_permalink($the_page->ID) . '">Popular</a>';

阅读有关模板层次结构的内容也会有所帮助。