【问题标题】:Filter posts in a WordPress loop with custom conditions使用自定义条件过滤 WordPress 循环中的帖子
【发布时间】:2015-12-07 03:40:19
【问题描述】:

我正在尝试在我的网站上构建一个后期过滤器。帖子页面需要在页脚显示两个相似的帖子。但他们必须满足某些条件。

条件:

  • 第一个条件:一个帖子有一个数值(例如:160),两个相似的帖子必须在该值的范围内(例如:如果范围是 10,一个帖子有一个155 值将在 160 范围内)。

  • 第二个条件:相似帖子与主帖属于同一类别

  • 第三个条件:如果前两个条件不成立,则显示预定帖子

循环:

  • 如果第一个条件产生两个结果,则循环必须停止。

  • 如果 第一个条件 只产生一个结果,则循环必须继续第二个条件,以便显示两个结果(第一个帖子匹配第一个条件,第二个帖子匹配 第二个条件)。

  • 如果第一个条件循环没有产生任何结果,则对两个帖子都使用第二个条件

  • 如果第一个第二个失败使用第三个

问题:

-循环不会遍历第一个条件的所有帖子,它只会遍历前两个最新帖子。在继续下一个条件之前,我怎样才能使条件先通过所有帖子?

有人可以向我解释如何实现我的目标或至少为我指明正确的方向。在此先感谢

到目前为止我得到了什么(宽度 cmets):

 <?php

    $orig_post = $post;
    $orig_city = get_post_meta( $post->ID, 'property_city', true );

    // The post range value for main post
    $orig_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $post->ID, 'property_area', true ) );

    // The post category for main post
    $orig_category = wp_get_post_terms( $post->ID, 'property_category', array( 'fields' => 'ids' ) );


    $exclude_ids = array( $post->ID );

    // display all posts
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'property',
        'post_status' => 'publish',
        'post__not_in' => $exclude_ids,

    );


    if ( $my_query->have_posts() && $orig_category ) {

        // Counter to display only two posts
        $counter = 0;
        while ( $my_query->have_posts() and $counter < 1 ) {
            $counter++;
            $my_query->the_post();
            $s_id = get_the_ID();

            // The range
            $s_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $s_id, 'property_area', true ) );
            $min = $orig_area - 10;
            $max = $orig_area + 10;

            // The first condition
            if ( ( $min <= $s_area ) and ( $s_area <= $max ) ) {
                echo 'first condition met';
                // The second condition
            } elseif ( $orig_category ) {
                echo 'second condition met';
                // The third condition
            } else {
                echo 'third condition met';
            }
            ?>

        <?php } ?>


    <?php }

    $post = $orig_post;
    wp_reset_query();
    ?>

【问题讨论】:

    标签: php wordpress if-statement conditional


    【解决方案1】:

    分成两个循环:

    $matches = [];
    $total = [];
    
    while ( $my_query->have_posts() ) {
    
        $post = $my_query->the_post();
    
        if (conformsToFirst($orig_post, $post)) 
            $matches[] = $post;
        else
            $total[] = $post;
    
        if (enough($orig_post, $matches))
            break;
    
    }
    
    if (!enough($orig_post, $matches)) {
        foreach ($total as $post) {
            if (conformsToSecond($orig_post, $post))
                $matches[] = $post;
    
            if (enough($orig_post, $matches))
                break;
        }
    
        if (!enough($orig_post, $matches)) {
    
            $matches = [pick_predefined_post()];
        }
    }
    
    // filtered results
    // either [first_condition, first_condition]
    // [first_condition, second_condition]
    // [second_condition, second_condition, ]
    // or [pick_predefined_post()-ed]
    var_dump($matches);
    
    
    function enough($to, $matches) {
        return count($matches) >= 2;
    }
    
    function conformsToFirst($to, $post) {
        //
        if (...)
            return true;
    
        return false;
    }
    
    function conformsToSecond($to, $post) {
        //
        if (...)
            return true;
    
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      相关资源
      最近更新 更多