Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示通过url传递ID的所有帖子_Php_Html_Wordpress - Fatal编程技术网

Php 显示通过url传递ID的所有帖子

Php 显示通过url传递ID的所有帖子,php,html,wordpress,Php,Html,Wordpress,我想得到wordpress中所有的帖子,它们的ID都是通过参数传递的,比如 ?pid=181109,5,1 我的front-page.php有以下代码 <?php while (have_posts()) : the_post(); ?> <?php if ('posts' == get_option('show_on_front')) { get_template_part

我想得到wordpress中所有的帖子,它们的ID都是通过参数传递的,比如 ?pid=181109,5,1

我的front-page.php有以下代码

<?php while (have_posts()) : the_post(); ?>
                <?php
                if ('posts' == get_option('show_on_front')) {
                    get_template_part('content', 'posts');
                } else {
                    get_template_part('content', 'page');
                }
                // If comments are open or we have at least one comment, load up the comment template
                if (comments_open() || '0' != get_comments_number()) :
                    comments_template();
                endif;
                ?>
            <?php endwhile;} // end of the loop.   ?>

我尝试了查询帖子('p=181')带有的单篇文章;它很好用。
但我无法确定如何为query_posts提供通过url传递的多个post id。需要帮助。

您可以尝试上述代码中的代码来解决您的问题

<?php 
$all_posts = sanitize_text_field($_GET['pid']);
if(!empty($all_posts)){
    $all_posts = explode(',', $all_posts);
}else{
    $all_posts = array();
}
$args = array( 
            'post_type' => 'post',
            'post__in' => $all_posts
        );
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        get_template_part('content', 'posts');
        if (comments_open() || '0' != get_comments_number()) :
            comments_template();
        endif;
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    echo "no posts found";
}
?>

我使用了以下代码

$thePostIdArray = explode(',', $_GET['pid']);
             $args = array(
                   'post__in'      => $thePostIdArray
                );

                query_posts($args);?>
             <?php while (have_posts()) : the_post(); ?>
                <?php
                if ('posts' == get_option('show_on_front')) {
                    get_template_part('content', 'posts');
                } else {
                    get_template_part('content', 'page');
                }
$thepostidaray=explode(',',$_GET['pid']);
$args=数组(
'post_uuin'=>postidaray$
);
查询_帖子($args);?>

我想你在找@DouwedeHaan,我试过$args=array('post_uin'=>array(43,23,65));但是我不明白如何从url获取帖子ID,因为你能修改url吗?因为如果你想要一个数组,你应该使用类似于
?pid[]=181&pid[]=109&pid[]=5&pid[]=1的东西。否则,一个简单的分解就足够了:
explode(',',$\u GET['pid'])
,它返回一个ID为'sthanks的数组,以获取帮助@DouwedeHaan