1

I'm trying to use bootstrap tabs in my index.php theme to show diferent content in the main page theme. I've implemented the tabs in index.php and create new page called popular-post.php linked in the tab Popular.

But when I click in the link to show popular content I get

Fatal error: Call to undefined function get_header() in

This is the code of my 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(); ?>

An this is the code of 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(); ?>

Thanks in advance

4

1 回答 1

2

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

创建一个新页面“Popular Posts”,选择模板(您的文件已经有一个页面模板标题。记下页面 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>';

阅读模板层次结构也会有所帮助。

于 2013-09-01T13:09:50.830 回答