链接新的 php 页面 wordpress [英] Link new php page wordpress

查看:26
本文介绍了链接新的 php 页面 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 index.php 主题中使用引导标签来显示主页主题中的不同内容.我已经在 index.php 中实现了选项卡,并创建了名为 Popular-post.php 的新页面,链接到了Popular"选项卡中.

但是当我点击链接显示热门内容时,我得到了

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

这是我的index.php代码

 <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="#">首页</a></li><li><a href="<?php bloginfo('template_directory'); ?>/popular-post.php">Popular</a></li><li><a href="#">Recientes</a></li><?php if ( have_posts() ) : while ( have_posts() ) : the_post();?><?php/* 包括内容的 Post-Format 特定模板.* 如果您想在子主题中重载它,请包含一个文件* 称为 content-___.php(其中 ___ 是 Post Format 名称),它将被使用.*/get_template_part('内容', get_post_format());?><?php endwhile;?><?php/* Pagnavi 插件支持 */wp_pagenavi();?><?php 其他:?><?php get_template_part('无结果', 'index');?><?php endif;?>

<div class="col-sm-4 col-md-4 col-lg-4" id="secondary"><?php get_sidebar();?>

</div><!--/content--><?php get_footer();?>

这是popular-post.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><a href="<?php bloginfo('template_directory'); ?>">首页</a></li><li class="active"><a href="#">Popular</a></li><li><a href="#">Recientes</a></li><ul class="popular_posts"><?php $pc = new WP_Query('orderby=comment_count&#038;posts_per_page=10');而 ($pc->have_posts()) : $pc->the_post();?><li><a href="<?php the_permalink(); ?>"title="<?php the_title(); ?>"><?php the_title();?></a><p>发布者<strong><?php the_author() ?></strong>与 <?php comments_popup_link('No Comments;', '1 Comment', '% Comments');?></p></li><?php endwhile;?>

<div class="col-sm-4 col-md-4 col-lg-4" id="secondary"><?php get_sidebar();?>

</div><!--/content--><?php get_footer();?>

提前致谢

解决方案

您不能直接链接到主题文件,例如:<a href="<?php bloginfo('template_directory'); ?>/popular-post.php">流行</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>';

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

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

解决方案

You can't link to a theme file directly like: <a href="<?php bloginfo('template_directory'); ?>/popular-post.php">Popular</a>.
This is an invalid URL: http://example.com/wp-content/themes/YOUR-THEME/any-theme-file.php.

Create a new page, "Popular Posts", select the template (your file already has a Page Template header. Take note of the page ID (in the URL). And link like:

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

This produces a valid URL: http://example.com/popular-posts/, which is a piece of your content (a page), that uses the defined page template file.

To get the page ID by its title use get_page_by_title:

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

Reading about the Template Hierarchy will also help.

这篇关于链接新的 php 页面 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆