在同一站点(WP)中的两种不同情况下使用外部循环

时间:2014-10-18 17:53:09

标签: php html wordpress loops blogs

在我的主题中,我有一个博客页面和一个主页(没有博客循环)。现在我想在我的主页上添加一些博客文章(就像最近一样)。

我正在使用博客页面作为主页的模板(我复制并重命名了原始版本),效果很好。但是我不能在不改变原始博客页面的情况下对其进行任何更改(循环在外部php中调用)。

我不知道要开始。

这是主页上调用博客循环的部分:

    <?php if(have_posts()) : the_post(); ?>

        <div id="main">
            <div class="central-wrapper clearfix">
                <div id="center" class="fullwidth">

                    <div id="content">
                        <?php if($gallery_select && $gallery_position == 1) : ?>
                            <?php codeus_gallery($gallery_select, $gallery_size, $gallery_style);  ?>
                        <?php endif; ?>

                        <div class="inner">
                            <?php the_content(); ?>
                            <?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
                            <?php if($quickfinder_position == 2) : ?>
                                <?php codeus_quickfinder($quickfinder_select); ?>
                            <?php endif; ?>

                            <?php if($portfolio_position == 2) : ?>
                                <?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
                            <?php endif; ?>

                            <?php if($gallery_select && $gallery_position == 3) : ?>
                                <?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
                            <?php endif; ?>

                        </div>
                    </div><!-- #content -->
                </div><!-- #center -->
            </div><!-- .central-wrapper -->
        </div><!-- #main -->

    <?php endif; ?>

</div><!-- wrap end -->

这是外部php中名为blog.php

的部分
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
    <ul class="styled">
        <?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
            <li class="clearfix">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),      'codeus_post_list_image'); ?>
                    <div class="comment-info">
                        <a href="<?php echo get_permalink($post->ID); ?>" class="date-day"><?php echo get_the_date('d'); ?></a>
                        <div class="date-month"><?php echo get_the_date('M'); ?></div>
                    </div>
                    <div class="post-info">
                        <h3><a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>
                        <?php if($image_url[0]) : ?>
                            <div class="post-image">
                                <div class="image wrap-box shadow middle">
                                    <div class="shadow-left"></div><div class="shadow-right"> </div>
                                    <a href="<?php echo get_permalink($post->ID); ?>"><img src="   <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="text clearfix"><?php the_excerpt(); ?></div>
                        <?php codeus_author_info(get_the_ID()); ?>
                    </div>
                </div>
            </li>
            <?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
        <?php endwhile; ?>
        </ul>
        <?php codeus_pagination($page_num, $pages_count); ?>
    </div>
<?php endif; ?>

请记住,我不想在原始blog.php中进行更改,因为正常的博客页面需要像现在一样工作。我想在主页上也开展博客,但最多只能展示3个帖子,可能属于某个类别。

欢迎帮助!!

1 个答案:

答案 0 :(得分:0)

我想我找到了一个解决方案,我编辑了这样的主页模板:

     <!-- wrap start --><div class="content-wrap">

    <?php if(have_posts()) : the_post(); ?>

        <div id="main">
            <div class="central-wrapper clearfix">
                <div id="center" class="fullwidth">

                    <div id="content">


                        <div class="inner">
                            <?php the_content(); ?>
                            <?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">                              
<?php


$page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
$items_per_page = 2;
$blog_loop = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => $items_per_page,
    'paged' => $page_num
));
$pages_count = ceil($blog_loop->found_posts/$items_per_page);
global $post;
$portfolio_posttemp = $post;
?>
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
    <ul class="styled">
        <?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
            <li class="clearfix">
                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
                    <div class="comment-info">
                        <a href="<?php echo get_permalink($post->ID); ?>" class="date-day"><?php echo get_the_date('d'); ?></a>
                        <div class="date-month"><?php echo get_the_date('M'); ?></div>
                    </div>
                    <div class="post-info">
                        <h3><a href="<?php echo get_permalink($post->ID); ?>"><?php the_title(); ?></a></h3>
                        <?php if($image_url[0]) : ?>
                            <div class="post-image">
                                <div class="image wrap-box shadow middle">
                                    <div class="shadow-left"></div><div class="shadow-right"></div>
                                    <a href="<?php echo get_permalink($post->ID); ?>"><img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
                                </div>
                            </div>
                        <?php endif; ?>
                        <div class="text clearfix"><?php the_excerpt(); ?></div>
                        <?php codeus_author_info(get_the_ID()); ?>
                    </div>
                </div>
            </li>
            <?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
        <?php endwhile; ?>
    </ul>

</div>
<?php endif; ?>
</div>



                        </div>
                    </div><!-- #content -->
                </div><!-- #center -->
            </div><!-- .central-wrapper -->
        </div><!-- #main -->

    <?php endif; ?>

</div><!-- wrap end -->