【问题标题】:post loop values in a variable (functions.php)在变量中发布循环值(functions.php)
【发布时间】:2016-01-18 16:31:01
【问题描述】:

所以,我有以下帖子循环来显示帖子(wp functions.php)。

global $post;    
$args = array( 
    'post_type' => 'post',      
    'posts_per_page' => 3,  
    'orderby' => 'date',            
    'order' => 'DESC'
    );
$loop = new WP_Query( $args );
$id = get_the_ID();
while ( $loop->have_posts() ) : $loop->the_post(); 

$my_post['sample_id'] =  get_the_title( $id );  

endwhile; 
wp_reset_postdata();    

我需要在一个值数组中包含帖子的标题(例如 array[title1, title2, title3])。

但是,它只选择一个标题。

如何使$my_post 变量可以具有三个值?

(换句话说,我只想将三个标题作为变量的结果)

编辑:

....
while ( $loop->have_posts() ) : $loop->the_post();   
array_push($titleArray, get_the_title($id));    
endwhile;
$tag_post['sample_id'] =  $titleArray;  
echo json_encode($tag_post);
wp_reset_postdata();
exit;   

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    最简单的方法是使用wp_list_puck()

    你可以试试下面的

    $args = [
        // Your arguments
    ];
    $q = get_posts( $args );
    $titles = wp_list_pluck( $q, 'post_title' );
    var_dump( $titles );
    

    【讨论】:

    • 我有兴趣使用它。我会试一下! =) 谢谢彼得
    • 没问题,这个方法比你有的更快,所以你应该考虑一下;-)
    • 我阅读了文档! =) 谢谢彼得。我正在实现这个方法,因为它只做我需要做的事情。
    • 我明白了。 =) 谢谢彼得!我在整个网站上阅读了很多你的答案,哦,伙计,你知道很多 wp 材料。哈哈。再次感谢您的帮助!
    • 如果你使用当前的 PHP 版本,你也可以试试array_column
    【解决方案2】:

    这是将所有标题存储到数组中的更新代码,

    global $post;    
    $args = array( 
        'post_type' => 'post',      
        'posts_per_page' => 3,  
        'orderby' => 'date',            
        'order' => 'DESC'
        );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
        $titleArray[] = get_the_title();
    endwhile; 
    
    print_r($titleArray); // Store to a variable as per your needs
    wp_reset_postdata();   
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的回复。我添加了一个编辑版本。结果我得到了null。我的编辑版本看起来还好吗?谢谢
    • @steveKim 你更新的代码对我来说很好用,输出如下:{"sample_id":["post1","post2","post3"]}
    • 你能指定你在函数中使用的钩子来渲染这段代码吗?
    • 完美。这是工作 ! =) 谢谢。欣赏!
    【解决方案3】:

    您无需执行任何操作,只需更改代码中的一行即可。

    替换它

    $my_post['sample_id'] =  get_the_title( $id );  
    

    有了它:

    $my_post['title'][] =  get_the_title( );  // $my_post will be your array containing all titles of your post.
    

    $my_post[] 将像 array_push 在那里。

    我希望它对你有用。

    【讨论】:

    • 我需要指定数据名称才能在前端使用它。我确实得到了数据,但我只得到一个值。
    • 然后只需使用多维数组只需将 $my_post[] 替换为 $my_post['title'][]
    【解决方案4】:

    例如:

    <?php 
    global $post;
    global $all_post_titles; //possibly needed//
    $all_post_titles = array();
    $args = array( 
        'post_type' => 'post',      
        'posts_per_page' => 3,  
        'orderby' => 'date',            
        'order' => 'DESC'
        );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); $all_post_titles[] = the_title('','',false); ?></a></li>
    <?php endforeach; ?>
    

    和:

    <?php 
      global $all_post_titles; //possibly needed//
      echo my_get_the_term_list( $post->ID, $taxonomy,  '', ' ', '', $all_post_titles );
    ?>
    

    根据第二个代码的位置,您可能不需要将$all_post_titles 声明为全局变量。

    如果您希望将所有发布数据放入数组中,那么此代码将对您有所帮助。

    $query = new WP_Query(array('post_type' => 'post'));
    $posts = $query->get_posts();
    $postArray = array();
    foreach($posts as $post) {
        // Do your stuff, e.g.
        // $postArray[] = $post->post_name;
        // store data to array;
    }
    

    【讨论】:

    • 感谢您的回复。有没有办法简单地获取数据并将它们保存在变量中?我对原始帖子进行了编辑。
    • 谢谢!我也能够使它与您的答案一起使用! =) 真的很感激! :)
    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2015-03-22
    相关资源
    最近更新 更多