Products
GG网络技术分享 2025-03-18 16:14 0
WordPress的图片如果是在发布、编辑时上传的,那么是有一个隶属于关系,即:图片是隶属于这一篇文章。我们可以通过这种隶属关系将文章中的所有图片找出来。
在single.php页面的the_loop代码片段中加入:
<?php
$args = array(
'post_type' => 'attachment', // 属于附件类型
'numberposts' => -1, // 查出所有内容
'post_status' => null, // 发布状态
'post_mime_type' => 'image', // 附件类型为图片
'post_parent' => $post->ID // 隶属于这篇文章
);
$attachments = get_posts( $args );
// 如果存在
if ( $attachments ) {
// 循环输出
foreach ( $attachments as $attachment ) {
var_dump($attachment);
}
}?>下图就是循环输出文章中的图片截图:
让WordPress一个页面显示全部分类的文章
1、复制一个page.php文件改为page-abc.php,并在WordPress后台新建一个页面,固定链接地址改为abc(这个abc可随意,但必须跟page-abc相对应)。
2、在这个page-abc.php文件中找到以下代码
<?php the_content(); ?> |
并在该代码后面添加以下代码
<?php $cats = get_categories(); foreach ( $cats as $cat ) { query_posts( \'showposts=10&cat=\' . $cat->cat_ID ); ?> <h3><?php echo $cat->cat_name; ?></h3> <ul class=\"sitemap-list\"> <?php while ( have_posts() ) { the_post(); ?> <li><a href=\"<?php the_permalink(); ?>\"><?php the_title(); ?></a></li> <?php } wp_reset_query(); ?> </ul> <?php } ?> |
记得保存更新page-abc.php文件。
到这里,我们刷新一下刚才新建的这个abc页面看看是否已经显示全部分类下的文章了?以上代码默认显示每个分类的10篇文章,如果需要显示所有文章,只需要把代码中的10改为1000或更大的数值即可。
让WordPress一个页面显示几个分类的文章
这个显示几个分类的文章的实现方法跟显示所有分类文章的方法是一样的,只需要将第2步中的代码
$cats = get_categories(); |
改为
$cats = get_categories(array(\'include\' => \'1,2,3\')); |
即可,其中1,2,3就是想要显示的分类ID。
以上就是wordpress如何在一个页面显示所有文章的详细内容,更多请关注网站的其它相关文章!
Demand feedback