WP_Query更改代码

时间:2017-07-05 02:56:48

标签: wordpress

在我的主题WordPress中显示帖子使用它(代码#1):

function inner($posts, $td_column_number = '') {
    $buffy = '';

    if (!empty($posts)) {
        foreach ($posts as $post) {
            $td_module_17 = new td_module_17($post);
            $buffy .= $td_module_17->render($post);
        }
    }

    return $buffy;
}

我需要使用此代码(代码#2)显示帖子:

<?php
$custom_query = new WP_Query('posts_per_page= 5');
while($custom_query->have_posts()):
    $custom_query->the_post();
    ?>
    <div class="post-1">
        <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="float-tags">
                <div class="bb-td-post-small-box clearfix">
                    <?php the_tags('','',''); ?>
                </div>
            </div>
            <div class="post-2">
                <?php the_content(); ?>
            </div>
        </div>
    </div>
    <?php
endwhile;
wp_reset_postdata(); // reset the query
?>

如果我将代码#2插入到模块17中,那么这些帖子会立即输出到一个块中,而不是一次输出一个。

1 个答案:

答案 0 :(得分:0)

解决了一个问题。代码是循环的。这是工作代码。

   <?php

    class td_module_17 extends td_module {

       function __construct($post) {
           //run the parrent constructor
           parent::__construct($post);



    }

    function render($Post = false) {
       ob_start();
    ?>

                 <div class="post-1">
                   <div

 <?php //post_class(); ?> id="post-<?= $Post->ID; ?>">          
                 <h3><a href="<?= get_permalink($Post->ID); ?>"><?= $Post->post_title; ?></a> </h3>    
                    <div class="float-tags">          
                                   <div class="bb-td-post-small-box clearfix">        
                                     <?php $tags = get_the_tags($Post->ID); ?>
                                     <? foreach ($tags as $tag) : ?>
                                            <a href="http://prikol2.ru/tag/<?= $tag->name; ?>/" rel="tag"><?= $tag->name; ?></a>
                                     <? endforeach; ?>
                                   </div>
                    </div>
                 <div class="post-2">                                            
                   <?= apply_filters('the_content', $Post->post_content); ?>    
                 </div>

               </div>
             </div>  


   <?php return ob_get_clean(); 
}
}