【问题标题】:Wordpress theme options page query_postsWordpress 主题选项页面 query_posts
【发布时间】:2010-11-24 13:48:44
【问题描述】:

我正忙于为一位客户构建一个小型主题选项页面,需要一些帮助来解决问题。

目前我可以选择手动输入 wordpress 页面的 IDS 以使用 query_posts 提取数据

基于主题选项创建一个名为$euro_box_1_vehicles;的变量

我的选项在输入中填写为32,39,43,54,当我用echo打印这个语句时,我得到了相同的结果。

当我将array(32,39,43,45) 替换为array($euro_box_1_vehicles) 时,它只返回一个结果。

<?php
    $vehicle1 = array(
        'post__in' => array(32,39,43,45),
        'post_type' => 'page',
    );

    query_posts( $vehicle1 ); 
    while (have_posts()) : the_post(); 
?>

【问题讨论】:

  • 试试var_dump($euro_box_1_vehicles),让我们知道打印的内容。
  • 当我回显 var_dump = string(11) "32,39,43,45"
  • @TheDeadMedic,为您添加了结果。

标签: wordpress wordpress-theming


【解决方案1】:

当我回显 var_dump = string(11) "32,39,43,45"

在这种情况下,您需要explode $vehicle1,因为post__in 需要一个数组;

query_posts(array(
    'post_type' => 'page',
    'post__in' => @explode(',', $vehicle1)
));

【讨论】:

    【解决方案2】:

    更新

    当我将 array(32,39,43,45) 替换为 array($euro_box_1_vehicles) 时,它只返回一个结果。

    您不应该将array(32,39,43,45) 替换为$euro_box_1_vehicles array($euro_box_1_vehicles)吗?后者似乎会用一个参数创建一个嵌套数组,即 array(array(32,39,43,45))。这不是你想要的。


    旧答案....

    如果我没看错,那么 query_posts() 需要一个 ID 列表? (32,39,43,45)

    但是当你传递给它 $vehicle1 时,你并没有给它一个 ID 列表,而是一个二维数组。

    <?php
        $vehicle1 = array(
            'post__in' => array(32,39,43,45),
            'post_type' => 'page',
        );
    
        query_posts( $vehicle1['post_in'] ); //use sub-array that contains list
        while (have_posts()) : the_post(); 
    ?>
    

    【讨论】:

    • 不,Eddie,@ApPel 做对了 - query_posts() 需要一个参数数组,post__in 是一个需要 ID 数组的特定参数。
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2013-02-19
    相关资源
    最近更新 更多