2014-12-04 48 views
0

我已经找过这个问题,我以前没见过它。如果这是一个重复的问题,我很抱歉。从帖子输入the_loop()ID

我已经完成了一个复杂的查询到我的自定义数据库表,我现在有一个WordPress的帖子ID数组。

现在我想向他们展示the_loop,但我无法做到这一点。我认为重要的是告诉你我正在用ajax显示结果,所有这些功能都在functions.php中(也许这些信息是无关紧要的)。

现在我得到的职位与数据:

function imprimirResultados($resultados = null){ 
    if ($resultados){ 
     echo '<div class="resultados">'; 
     foreach ($resultados as $post_id){ 
      $post = get_post($post_id[0], ARRAY_A); 
      echo '<div class="post" id="post-'.$post['ID'].'"> 
        <h2><a href="'.$post['guid'].'" rel="bookmark" title="Permanent Link to '.$post['post_name'].'"> 
        '.$post['post_title'].'</a></h2> 
        <div class="entry"> 
        '.$post['post_excerpt'].' 
        </div> 
       </div>'; 
     } 
     echo '</div>'; 
     var_dump($post); 
    }else{ 
     echo '<h2 class="center">No encontrado</h2> 
       <p class="center">Lo sentimos, pero la búsqueda no obtuvo resultados.</p>'; 
    } 
} 

但它不是一个干净的方法,这样做,我不能使用其他功能,通过the_loop生成的对象()已经有。

---------------------------------编辑------------ -------------------

我离开这里的功能我用:

function resultadosLigeros_callback(){ 

    (...) 
    $querystr = cargar_ligeros($marca,$modelo,$combustible,$tipo,$anio); //This function generate a MySQL query 
    $resultados = $wpdb->get_results($querystr, ARRAY_N); 
    imprimirResultados($resultados, $querystr);//This function is the one I wrote before 

    die(); 
} 
+0

'the_loop()'不是一个WordPress的功能...你可以显示你的功能,以及它的钩子? – rnevius 2014-12-04 10:43:15

回答

1

你要生成一个新的WP_Query的基础上,你有的帖子ID。具体来说,您需要使用post__in

例如,由于$resultados是一个数组:

<?php 
    // Fetch the posts 
    $query = new WP_Query(array('post__in' => $resultados)); 

    // The Loop 
    while ($query->have_posts()) : $query->the_post(); 
?> 

然后可以使用的功能,例如the_ID()等,直接在模板的逻辑。

+1

是啊!你救了我的屁股! – user3592436 2014-12-04 11:13:14