将不同类别的帖子拉到不同的页面上(Wordpress)(pulling posts from different categories on to different pages (Wordpress

编程入门 行业动态 更新时间:2024-10-26 13:29:57
将不同类别的帖子拉到不同的页面上(Wordpress)(pulling posts from different categories on to different pages (Wordpress))

我有这个代码:

<?php $catPost = get_posts(get_cat_ID("music")); //change this foreach ($catPost as $post) : setup_postdata($post); ?> <div> <li id="product_thumbnails"> <h2><?php the_title(); ?></h2> <p><?php $thumbnail_id=get_the_post_thumbnail($post->ID); preg_match ('/src="(.*)" class/',$thumbnail_id,$link); ?>

它将所有帖子从“MUSIC”类别拉到特定页面。 我有大约15页,我想让他们每个人拉出不同的类别,如“电影,体育等”。 我是否必须创建15个模板,并在每个模板中更改此行:

$catPost = get_posts(get_cat_ID("music")); //change this

举例来说:

$catPost = get_posts(get_cat_ID("movies")); //change this

或者有一种更清洁的方法,它有可能吗? 我正在使用主题二十二。

谢谢!

i have this code:

<?php $catPost = get_posts(get_cat_ID("music")); //change this foreach ($catPost as $post) : setup_postdata($post); ?> <div> <li id="product_thumbnails"> <h2><?php the_title(); ?></h2> <p><?php $thumbnail_id=get_the_post_thumbnail($post->ID); preg_match ('/src="(.*)" class/',$thumbnail_id,$link); ?>

which pulls all posts from "MUSIC" category onto specific page. I have around 15 pages and i want onto each one of them to pull different category like "Movies, Sports etc". Do i have to create 15 templates and into each one of them change this line:

$catPost = get_posts(get_cat_ID("music")); //change this

to for example:

$catPost = get_posts(get_cat_ID("movies")); //change this

or there is a cleaner way to do it, is it possible at all? I am using theme twenty twelve.

Thanks!

最满意答案

你可以做的是创建你的十五页,用下面的代码制作一个模板,然后添加一个custom field ,可能像this_page_cat (你可以命名它)到所有十五页。 所以对于你的十五页你会有:

page 1: this_page_cat = music page 2: this_page_cat = books page 3: this_page_cat = movies ...

然后只需将模板中的代码更改为

<?php $custom_fields = get_post_custom(); //gets custom fields for this page $my_custom_field = $custom_fields['this_page_cat']; //get fields named 'this_page_cat' $catPost = get_posts('category=' . get_cat_ID($my_custom_field[0])); //get_post_custom() returns multidimensional array so you need to access the index you want foreach ($catPost as $post) : setup_postdata($post); ?> <div> <li id="product_thumbnails"> <h2><?php the_title(); ?></h2> <p><?php $thumbnail_id=get_the_post_thumbnail($post->ID); preg_match ('/src="(.*)" class/',$thumbnail_id,$link); endforeach; ?>

将该模板应用于所有十五页,你应该是好的。 自定义字段是WP最好的部分之一,这里有更多信息 。 另一个答案可能会奏效,但这是一个更具可扩展性的解决方案。

what you can do is create your fifteen pages, make one template with the below code, and just add a custom field, maybe something like this_page_cat (you can name it anything) to all fifteen pages. so for your fifteen pages you would have:

page 1: this_page_cat = music page 2: this_page_cat = books page 3: this_page_cat = movies ...

then just change your code in the template to

<?php $custom_fields = get_post_custom(); //gets custom fields for this page $my_custom_field = $custom_fields['this_page_cat']; //get fields named 'this_page_cat' $catPost = get_posts('category=' . get_cat_ID($my_custom_field[0])); //get_post_custom() returns multidimensional array so you need to access the index you want foreach ($catPost as $post) : setup_postdata($post); ?> <div> <li id="product_thumbnails"> <h2><?php the_title(); ?></h2> <p><?php $thumbnail_id=get_the_post_thumbnail($post->ID); preg_match ('/src="(.*)" class/',$thumbnail_id,$link); endforeach; ?>

apply that template to all fifteen pages and you should be good. Custom fields are one of the best parts of WP, here is more info on them. The other answer would probably work but this is much more extensible solution.

更多推荐

本文发布于:2023-08-04 19:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将不   拉到   同类   页面   帖子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!