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 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000