如何通过wordpress中的字段过滤自定义帖子?
好的,非常简单的任务,我只是不擅长PHP。如何通过wordpress中的字段过滤自定义帖子?
我有一个页面,我想列出一些使用样式列表的工作人员。这是网页 - http://www.themontessoripeople.co.uk/montesori/?post_type=people
我下载了一个“自定义内容类型”插件,并添加了“people”的内容类型并添加了相应的字段。现在我想筛选我通过名为“层次结构”的自定义字段添加的帖子。
这里是我想要的页面显示 - http://i47.tinypic.com/oqymwh.jpg
自定义字段“层次”既包含“管理”,“babies_room”和“toddlers_room”的房间变量。
如何修改下面的代码以过滤<?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; ?>
谢谢,本。
这里是在参考了两套筛选结果的工作代码 -
<?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 -->
我已经简化你的代码。添加过滤器:
<?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);
}
?>
编辑:get_custom_field而不是print_custom_field。
嘿安德烈亚斯,感谢您的时间。我将代码添加到模板文件,并在浏览到页面时未显示。我试着将过滤器添加到原始代码中,并且它只是在每个帖子旁边打印值。我想也许这可能是一个简单的语法错误,但DW没有返回。如果你想再给它一次,我可以给你登录测试网站,你可以消除我搞砸它的机会。测试已经完成了一切测试。再次感谢,本。 –
请尝试评论// if($ hierarchy)部分 –
我修复了代码。 –
您可以在query_posts($args)
中定义查询参数 - 查看query_posts。也许你可以试试get_posts
我想程序员知道怎么做......他说他不擅长php ... –
不要使用query_posts,blech。改为使用WP_Query类。 - http://codex.wordpress.org/Class_Reference/WP_Query – Xhynk
我认为这将得到更快的答复,如果它被问到在WordPress的堆栈交换站点 – Kristian
相信我,它没有。我已经问过两次了 - http://wordpress.stackexchange.com/questions/66216/how-do-i-filter-a-custom-post-type-loop-by-a-field:s –
wordpress stack exchange甚至没有像堆栈溢出那样有用。大多数WP问题都可以提出并“通用化”,只是一个编程问题(许多程序员知道WordPress的PHP平台如何工作的基础知识) – Xhynk