显示页面内容

问题描述 投票:0回答:2

我有两个代码。我在page.php上使用的第一个,它显示我在管理面板中创建的任何页面的内容。第二个代码用于在主页上显示我的帖子,只是不确定该代码应该去哪里。

它可以工作,如果我把它放在page.php中,但然后相同的内容(帖子)显示在我创建的任何页面上。我尝试将第二个代码放在home.php和index.php中,同时使用page.php中的第一个代码但不起作用。

if (have_posts()): while (have_posts()): the_post();
    wp_title(''); echo '<br />';
    the_content(); echo '<br />';
  endwhile; endif;

<--- Second Code -->

$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
  setup_postdata( $post ); ?>

  <div id="pbox">
    <div id="pthumb"><?php the_post_thumbnail(  array(100,100) ); ?></div>
        <div id="pcontent">
    <a href="<?php the_permalink(); ?>" class="ptitle"><?php the_title(); ?></a>
    <?php the_excerpt(); ?><br />

Post Category: <?php the_category( ', ' ); ?>



        </div>
    </div>

<?php endforeach; 
wp_reset_postdata(); ?>
wordpress
2个回答
1
投票

我想你应该看看以下内容:https://developer.wordpress.org/themes/basics/template-hierarchy/#home-page-display

您会注意到,如果存在,home.php是WP将用于显示您的帖子页面的文件 - 当然,如果在设置>阅读>帖子页面中设置了与您的正常首页分开的页面。

然后在home.php中,你可以添加你喜欢的任何东西,即 -

<?php
    $args = array(
    'show_option_all' => '',
    'orderby' => 'name'
    );                          
     wp_list_categories( $args );
?>

为了澄清,如果你的主题中不存在home.php,那么WP会查找index.php。


0
投票

page.php,single.php有相同的查询帖子,因为它只会获取内容。您可以通过以下方式区分。

主页显示在fallowing中添加相同的代码(查询帖子)

1)home.php 2)index.php

  • front-page.php - 用于设置→阅读的首页显示部分中设置的“您的最新帖子”或“静态页面”。
  • home.php - 如果WordPress找不到front-page.php并且在首页显示部分设置了“你的最新帖子”,它将查找home.php。此外,当在首页显示部分设置帖子页面时,WordPress将查找此文件。
  • page.php - 在首页显示部分设置“首页”时。 index.php - 当在首页显示部分设置“您的最新帖子”但home.php不存在或者设置了首页但是page.php不存在时。

单一帖子

单个帖子模板文件用于呈现单个帖子。 WordPress使用以下路径:

  • single- {post-type} - {slug} .php - (从4.4开始)首先,WordPress会查找特定帖子的模板。例如,如果post类型是product并且post slug是dmc-12,则WordPress会查找single-product-dmc-12.php。
  • single- {post-type} .php - 如果帖子类型是产品,WordPress会寻找single-product.php。
  • single.php - 然后WordPress回到single.php。
  • singular.php - 然后它回落到singular.php。
  • index.php - 最后,如上所述,WordPress最终会回落到index.php。

单页

用于呈现静态页面的模板文件(页面后置型)。请注意,与其他后期类型不同,页面对于WordPress是特殊的,并使用以下补丁:

  • 自定义模板文件 - 分配给页面的页面模板。请参阅get_page_templates()。
  • page- {slug} .php - 如果页面slug是recent-news,WordPress将使用page-recent-news.php。
  • page- {id} .php - 如果页面ID为6,WordPress将使用page-6.php。 page.php文件
  • singular.php
  • 的index.php

详细说明:click here

© www.soinside.com 2019 - 2024. All rights reserved.