duanhui4160 2015-07-16 12:39
浏览 29
已采纳

两个不同的get_posts()返回相同的帖子

I am using Wordpress and trying to create some boxes retrieving information from Adanvec Customs Fields on a custom post type.

I have this code:

<div class="col-sm-2 hidden-xs">
    <?php
    $box1 = get_front_page_box("Box 1");
    $style_front = get_box_style($box1->ID, "front");
    echo $box1->ID;
    ?>
    <div id="front-box-1" class="front-box height-low <?php echo $box1->ID; if(has_back_panel($box1->ID)) echo "flip"; ?>">
        <div class="front" style="<?php echo $style_front; ?>"><a href="#">Banana</a></div>
        <?php
        if(has_back_panel($box1->ID)):
            $style_back = get_box_style($box1->ID, "back");
        ?>
        <div class="back" style="<?php echo $style_back; ?>"></div>
        <?php endif; ?>
    </div>
</div>
<div class="col-sm-5 hidden-xs">
    <?php
    $box2 = get_front_page_box("Box 2");
    $style_front = get_box_style($box2->ID, "front");
    echo $box2->ID;
    ?>
    <div id="front-box-2" class="front-box height-low <?php echo $box2->ID; if(has_back_panel($box2->ID)) echo "flip"; ?>">
        <div class="front" style="<?php echo $style_front; ?>"><a href="#">Banana</a></div>
        <?php
        if(has_back_panel($box2->ID)):
            $style_back = get_box_style($box2->ID, "back");
        ?>
        <div class="back" style="<?php echo $style_back; ?>"></div>
        <?php endif; ?>
    </div>
</div>

And these functions:

function get_front_page_box($name) {
    $args = array(
        'post_title'   => $name,
        'post_type'    => 'front-page-box',
        'post_status'  => 'publish'
    );

    $box_array = get_posts($args);

    $box = $box_array ? $box_array[0] : false;

    print_array($box);
    return $box;
}

function get_box_style($id, $side) {
    $style = "";
    if(get_field($side."_panel_background_color", $id)) $style = "background-color:".get_field($side."_panel_background_color", $id).";";

    return $style;
}   

But for some reason both boxes have the same details (both from Box 2). Any idea why these are returning the same info? Both posts ("Box 1" and "Box 2" exist under the front-page-boxcustom post type.

  • 写回答

3条回答 默认 最新

  • dongqichang7988 2015-07-16 15:59
    关注

    Seems the post_title is not a valid filter. But found out there's a specific function to get posts by title.

    get_page_by_title

    Used $box1 = get_page_by_title("Box 1", NULL, "front-page-box");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?