我有这个代码:
<?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 thisto for example:
$catPost = get_posts(get_cat_ID("movies")); //change thisor 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.
更多推荐
发布评论