Wordpress自定义类别字段返回匹配的帖子类别
因此,我有以下查询来显示页面模板上的帖子。Wordpress自定义类别字段返回匹配的帖子类别
$wp_query = new WP_Query(); $wp_query->query('category_name=specials&posts_per_page=2' . '&paged='.$paged);
哪里有CATEGORY_NAME =特价我想特价是一个动态的现场获悉/从自定义字段获取网页...所以页面可能有一个自定义字段“类别”,例如,我可以输入它的特殊值。因此,该页面将显示与我在客户字段值中键入的类别匹配的所有帖子......这可能吗?
是,自定义字段的值赋给一个变量并在查询中使用它如下图所示:通过使用下面的代码
$custom_field = //get custom field value
$wp_query = new WP_Query(); $wp_query->query('category_name='.$custom_field.'&posts_per_page=2' . '&paged='.$paged);
$ custom_field = get_post_meta(get_the_ID(),'category',true); $ wp_query = new WP_Query(); $ wp_query-> query('category_name ='。$ custom_field。'&posts_per_page = 2'。'&paged ='。$ paged); 非常感谢尼尔,这就是我需要的。这是我使用过的自定义字段,效果很好! –
如何获得该类别明智的帖子?
添加类别ID和分类塞在参数
<?php
$post_type = 'post';
$page_paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $page_paged,
'caller_get_posts' => -1,
'orderby' => 'name',
'order' => 'DESC',
'pad_counts' => false,
'hierarchical' => 1,
'hide_empty' => 0,
'tax_query' => array(
array(
'taxonomy' => 'your taxonomy slug',
'field' => 'id',
'terms' => 'your category id'
)
),
);
$loop = new WP_Query($args);
while (have_posts()) : the_post();
the_title('<h2>', '</h2>', true);
the_content();
endwhile; // end of the loop.
wp_reset_query();
?>
谢谢Shital,这是一个很好的解决方案。 –
欢迎!乐于帮助 :) –
感谢尼尔一个快速的答案
这里是代码的完整的工作块。
在页面上添加自定义字段“类别”,然后确保此自定义字段的值与帖子类别相匹配,该类别将在页面上返回这些帖子。
<?php // Display blog posts with category filter from custom field
$temp = $wp_query; $wp_query= null;
$custom_field = get_post_meta(get_the_ID(), 'category', true);
$wp_query = new WP_Query(); $wp_query->query('category_name='.$custom_field.'&posts_per_page=2' . '&paged='.$paged);
$wp_query = new WP_Query(); $wp_query->query('category_name=specials&posts_per_page=2' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
<div class="next"><?php previous_posts_link('Newer Posts »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Previous Posts'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
所以你试图复制已经存在的类别页面/存档功能与你自己?为什么? – CBroe