Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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 foreach链接Wordpress_Php_Wordpress_Foreach_Posts - Fatal编程技术网

PHP foreach链接Wordpress

PHP foreach链接Wordpress,php,wordpress,foreach,posts,Php,Wordpress,Foreach,Posts,对我来说,循环使用wordpress帖子ID数组并生成链接图像的最佳方式是什么 我目前有: <?php $posts = array(1309,880,877,890,1741,1739,2017); print "<div class='row'>"; foreach($posts as $post){ $queried_post = get_post($post); echo "<a href='get_per

对我来说,循环使用wordpress帖子ID数组并生成链接图像的最佳方式是什么

我目前有:

<?php
    $posts = array(1309,880,877,890,1741,1739,2017);

    print "<div class='row'>";
    foreach($posts as $post){
        $queried_post = get_post($post);
        echo "<a href='get_permalink( $post )'>";
        print "<div class='col-xs-2'>";      
            echo get_the_post_thumbnail($queried_post->ID, 'thumbnail');
        print "</div>"; 
        print "</a>";
    }
    print "</div>";                  
?>

您可以使用如下内容:

<?php
    $posts = array(1309,880,877,890,1741,1739,2017);
?>
   <div class='row'>
    <?php foreach($posts as $post): ?>
       <?php $queried_post = get_post($post); ?>
        <a href="<?php echo get_permalink( $post ) ?>">
        <div class='col-xs-2'>     
        <?php echo get_the_post_thumbnail($queried_post->ID, 'thumbnail'); ?>
        </div>
       </a>
    <?php endforeach; ?>
   </div>  

如果你使用WordPress,你会经常遇到这种语法


如果您还没有,最好查看,它们提供了所有功能的示例等。由于该软件的使用非常广泛,它们倾向于坚持最佳实践(在大多数情况下!),因此可能非常有益。

您应该使用WP\u查询类

# The Query
$the_query = new WP_Query( $args );

# Open div.row
echo '<div class="row">';

# The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {

        $the_query->the_post(); ?>

        <a href="<?php the_permalink(); ?>">
            <div class="col-xs-2"><?php the_post_thumbnail( 'medium' ); ?></div>
        </a>

<?php }
} else {
    # no posts found
}

# Close div.row
echo '<div>';

# Restore original Post Data
wp_reset_postdata(); 
#查询
$thew_query=newwp_query($args);
#开放式分区行
回声';
#环路
if($the\u query->have\u posts()){
while($the\u query->have\u posts()){
$the_query->the_post();?>