简体   繁体   English

如何按Wordpress中的字段过滤自定义帖子?

[英]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.

相关问题 Wordpress 如何根据某些帖子类型中的自定义字段过滤帖子 - Wordpress how to filter posts basing on custom field in certain post type 如何查询帖子自定义关系字段并将解析的搜索词包含在循环中并在 WordPress 中显示结果 - How do I query posts custom relationship field and include parsed search term into loop and display results in WordPress Wordpress - 如何显示按自定义分类法分组的帖子? - Wordpress - How do I display posts grouped by custom taxonomy? 如何通过自定义字段日期订购wordpress帖子? - How to order wordpress posts by a custom field date? Wordpress 如何根据自定义字段对帖子进行排序 - Wordpress how to sort posts according to Custom Field 如何在自定义字段中按日期排列Wordpress帖子? - How to arrange Wordpress posts by date in a custom field? 如何按自定义月份字段对WordPress帖子进行排序? - How to sort WordPress posts by a custom month field? 如何在Wordpress中按自定义字段值排序帖子 - How to order posts by custom field value in wordpress Wordpress:筛选器发布基于URL参数的自定义字段值 - Wordpress: Filter Posts Custom Field Value Based on URL Parameter 如何在自定义Wordpress循环中隐藏过去的帖子并显示即将发布的X个帖子? - How do I hide past posts in a custom Wordpress loop AND display X number of upcoming posts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM