php - Wordpress 页面内容和帖子在同一页面中
问题描述
我是 wordpress 世界的新手,我正在尝试将 HTML 页面调整为 wordpress 主题。我需要首先在页面上显示页面内容,然后在该页面下显示帖子。但我得到的只是在页面上显示两次的帖子(页面内容应该在哪里)。有没有可能克服这个问题?
还有一个问题,如何根据类别过滤帖子?我已经尝试过query_posts('cat=Small')
,但它似乎无法正常工作。
代码index.php
如下:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
解决方案
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>
推荐阅读
- python - Python连接/合并一个文件中的一行
- python - 如何将使用 Keras 准备的图像分类器之类的对象保存到使用 Python 的任何文件中?
- c# - 如何重新验证视图控件?
- android - onActivityResult 未在 MainActivity 从上一个 Activity 中调用
- multithreading - 如何从 tcl 脚本运行多个实例
- sql - 如何使用 SQL 从数据库中获取键值对
- iphone - 新 Iphone 型号发布后的整页背景图像大小 (Xcode)
- java - 从服务器检索信息时出现应用内购买错误 [DF-AA-20] - android
- windows - 如何在 Windows 上从 USB 设备 VID 和 PID 获取供应商名称和产品名称?
- html - 其他元素堆叠在一起,我已经将它们的容器设置为 position:relative;