[英]How do I filter a custom posts by a field in wordpress?
OK, very simple task, I'm just not good at PHP. OK,非常简单的任务,我只是不擅长PHP。
I have a page where I want to list some staff using a styled list. 我有一个页面,我想使用样式列表列出一些人员。 Here is the page - http://www.themontessoripeople.co.uk/montesori/?post_type=people 这是网页-http: //www.themontessoripeople.co.uk/montesori/?post_type=people
I downloaded a "custom content type" plugin and added the content type of "people" and added the appropriate fields. 我下载了“自定义内容类型”插件,并添加了“人物”的内容类型并添加了相应的字段。 Now I want to filter the posts I have added by the custom field called "hierarchy". 现在,我想过滤由自定义字段“ hierarchy”添加的帖子。
Here is how I want the page to display - http://i47.tinypic.com/oqymwh.jpg 这是我希望页面显示的方式-http://i47.tinypic.com/oqymwh.jpg
The custom field "hierarchy" contains the room variables of either "management", "babies_room" and "toddlers_room". 定制字段“层次结构”包含“ management”,“ babies_room”和“ toddlers_room”的房间变量。
How do I amend the code below to filter the posts by the value held within <?php print_custom_field('hierarchy'); ?>
我如何修改下面的代码以按<?php print_custom_field('hierarchy'); ?>
的值过滤帖子<?php print_custom_field('hierarchy'); ?>
<?php print_custom_field('hierarchy'); ?>
? <?php print_custom_field('hierarchy'); ?>
<?php $col = 1; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ($col == 1) echo "<div class=\"row\">"; ?>
<div class="post col<?php echo $col;?>" id="post-<?php the_ID(); ?>">
<div class="people-spacer">
<div class="people"><a class="animate" >
<div class="bio">
<p class="titles"><?php the_title(); ?><br/>
<span class="job"> <?php print_custom_field('job'); ?></span> </p><br />
</div>
<img src="<?php print_custom_field('staff_image:to_image_src'); ?>" width="160" height="160" alt="<?php the_title(); ?>-image" />
</div>
<div class="people-link-edit"><?php edit_post_link('Edit Post', ''); ?></div>
</div>
</div>
<?php if ($col == 1) echo "</div>"; (($col==1) ? $col=2 : $col=2); ?>
<?php endwhile; ?>
Thanks, Ben. 谢谢,本
Here is the working code showing two sets of filtered results for reference - 这是工作代码,显示了两组经过过滤的结果供参考-
<?php $col = 1; ?>
<?php if (have_posts()) : ?>
<div class="text-box">
<h2>Management</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if (get_custom_field('hierarchy') != "management") continue; ?>
<?php if ($col == 1) echo "<div class=\"row\">"; ?>
<div class="post col<?php echo $col;?>" id="post-<?php the_ID(); ?>">
<div class="people-spacer">
<div class="people"><a class="animate" >
<div class="bio">
<p class="titles">
<?php the_title(); ?>
<br/>
<span class="job"> <?php print_custom_field('job'); ?></span> </p>
<br />
</div>
<img src="<?php print_custom_field('staff_image:to_image_src'); ?>" width="160" height="160" alt="<?php the_title(); ?>-image" /> </div>
<div class="people-link-edit">
<?php edit_post_link('Edit Post', ''); ?>
</div>
</div>
</div>
<?php if ($col == 1) echo "</div>"; (($col==1) ? $col=2 : $col=2); ?>
<?php endwhile; ?>
</div><!-- close text box -->
<div class="text-box">
<h2>Babies Room</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if (get_custom_field('hierarchy') != "babies_room") continue; ?>
<?php if ($col == 1) echo "<div class=\"row\">"; ?>
<div class="post col<?php echo $col;?>" id="post-<?php the_ID(); ?>">
<div class="people-spacer">
<div class="people"><a class="animate" >
<div class="bio">
<p class="titles">
<?php the_title(); ?>
<br/>
<span class="job"> <?php print_custom_field('job'); ?></span> </p>
<br />
</div>
<img src="<?php print_custom_field('staff_image:to_image_src'); ?>" width="160" height="160" alt="<?php the_title(); ?>-image" /> </div>
<div class="people-link-edit">
<?php edit_post_link('Edit Post', ''); ?>
</div>
</div>
</div>
<?php if ($col == 1) echo "</div>"; (($col==1) ? $col=2 : $col=2); ?>
<?php endwhile; ?>
</div><!-- close text box -->
I've simplified your code. 我简化了您的代码。 The filter is added too: 过滤器也被添加:
<?php
$col = 1;
while (have_posts())
{
the_post();
if ($col == 1) echo "<div class=\"row\">";
// filter
$hierarchy = get_custom_field('hierarchy');
// if it does not match continue (skip)
if ($hierarchy != "boss") continue;
// if it matches continue (skip)
//if ($hierarchy == "notboss") continue;
// needed fields
$id = the_ID();
$job = get_custom_field('job');
$title = the_title();
$img = get_custom_field('staff_image:to_image_src');
$edit = edit_post_link('Edit Post', '');
echo <<< END
<div class="post col$col" id="post-$id">
<div class="people-spacer">
<div class="people"><a class="animate" >
<div class="bio">
<p class="titles">$title<br/>
<span class="job">$job</span> </p><br />
</div>
<img src="$img" width="160" height="160" alt="$title-image" />
</div>
<div class="people-link-edit">$edit</div>
</div>
</div>
END;
if ($col == 1) echo "</div>";
(($col==1) ? $col=2 : $col=2);
}
?>
Edit: get_custom_field instead of print_custom_field. 编辑:get_custom_field而不是print_custom_field。
You can define query args in query_posts($args)
- have a look at query_posts . 您可以在query_posts($args)
定义查询query_posts($args)
看一下query_posts 。 Maybe you can try get_posts
也许你可以尝试get_posts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.