对于您的第一个问题,wp 根据 query_posts() 抓取主页的标题,这就是 wordpress 如此动态的原因,您实际上可以检查页面或类别 ID,然后使用查询帖子选择要在其上显示的内容。由于您的代码中没有 query_posts,因此 wp 默认为当前的帖子 ID,即您的主页。
对于你的第二个,我有一个代码 sn-p,但它在家里,你可以在这里查找
https://developer.wordpress.org/reference/functions/query_posts/
好的,这里是sn-p。抱歉耽搁了,我一直在休假。
首先你要确定你的主页的ID,或者你可以使用主页功能。如果您走前一条路线,您只需转到主页的后端并用鼠标悬停在它上面。主页应显示在页面底部的链接工具提示中。这将类似于 post=5 这个数字就是你需要的。
接下来,我将为您要在首页上显示的帖子创建一个类别,并从后端的类别页面中获取该 ID。再次,它将类似于 tag_id=12
<?php
$currpage = $post -> ID;
if($currpage == 5) {
query_posts('showposts=1&cat=12');
}
while (have_posts()): the_post();
您可以通过这种方式在一个页面上放置多个帖子,实际上您可以在该页面上放置整个循环,然后在其下动态填充其他帖子,如下例所示:
http://testex-ndt.com/products/ect-products/ 顶部是循环工作的页面,底部(推荐)是分配给特定类别的帖子,此处的 showposts 设置为 3,并将显示最新三个帖子的摘录。这是我过去做的一个网站。
您可能要记住的另一件事是页面模板,如果您希望一个页面以一种方式工作,而您希望另一种类型的页面以另一种方式工作,您将不得不研究模板。
这是该页面的完整代码。
<?php
/*
Template Name:Main Product
*/
?>
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
<?php if (have_posts()) : ?>
<?php while (have_posts()): the_post(); ?>
<?php $postName = get_the_title($post); ?>
<!-- title link dynamic -->
<?php edit_post_link('Edit this item','',' | '); ?>
<?php the_content('Continue Reading'); ?>
<?php posts_nav_link(); ?>
<?php endwhile; ?>
<?php else : ?>
<div class="w3-col text">
<h2>Not Found</h2>
<p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
<!-- insert testimonial here -->
<section class="w3-row cl testimonials">
<!-- make code for query based on the page, then get the top three testimonials in excerpt form This is commented out because I did it with a function I attached to the header I will show below
$currpage = $post -> ID;
if($currpage == xx){
$cat = x;
}
-->
<h2>Testimonials for <?php echo $postName; ?></h2>
<?php
$currpage = $post -> ID;
$cat = testimonial($currpage); //here is the function
query_posts('showposts=3&cat=' .$cat ); ?>
<?php while (have_posts()): the_post(); ?>
<div class="w3-col excerpt" style="width:30%;">
<?php the_excerpt(''); ?>
</div>
<?php endwhile;
wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS
?>
</section>
<!-- /content float -->
</div>
<!-- /content -->
</div>
所以函数被设置为一个单独的 php 文件,所以我可以模块化地改变它。我调用了文件 category_help.php
<?php
/*This particular block of code is only for determining what category of testimonial is allowed in a post. */
function testimonial($myPageID){
//services (all cats)
$id = "-15,-14,-13";
//Products
//lfet
if($myPageID == 18) $id = 2;
//bfet
if($myPageID == 22) $id = 4;
//rfet
if($myPageID == 20) $id = 3;
//ect
if($myPageID == 24) $id = 5;
//ultrasound
if($myPageID == 26) $id = 8;
return $id;
}
?>
然后我在 header.php 的顶部调用了这个
<?php require 'category_help.php'; ?>