1

我在 Wordpress 网站上有这段代码,我希望对其进行修改,以便将所有内容分页成每页三个帖子。我该怎么做呢?任何对 PHP-noob 的帮助将不胜感激....

    <?php /* Template Name: News */ get_header(); ?>

    <div id="content">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php the_content(); ?>
    <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    <?php edit_post_link('[e]','<p>','</p>'); ?>
    <?php endwhile; endif; ?>



    <?php query_posts(array('showposts' => 22, 'post_type' => 'post', 'order' => 'desc', 'cat' => '1')); while (have_posts()) : the_post(); ?>


    <h6 style="text-align:left; border:none; margin:0px;"><?php the_title();?></h6>
    <?php the_content(); ?>
    <div class="sep"></div>


    <?php endwhile; wp_reset_query();?>  

    </div>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
4

1 回答 1

0

请不要使用query_posts:) 非常糟糕的做法。所以你希望这个循环得到 3 个帖子......

    <!--The-Loop-->
    <?php // Loop (Will get every post Except Sticky ones...)


    $sticky = get_option( 'sticky_posts' );
    /*Let's add pagination to post page and static page*/
    /*We need this here to add and maintain Pagination if Template is assigned to Front Page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }

$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,
     'post__not_in'  => $sticky,
     'ignore_sticky_posts' => 1,
           'posts_per_page' => 3
);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

 if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>



    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">

    <h3 class="mytitle"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'domain' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <?php if ( has_post_thumbnail() ):?>
                <div class="featured_img">
                <?php
                the_post_thumbnail();
                echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
                ?>
                </div><!--/featured_img-->
                <?php endif; ?>
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(__('Read more','domain')); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;','domain'); ?> <?php the_category(', ') ?> <?php _e('by','domain'); ?> <?php  the_author(); ?><br/><?php the_tags(__('Tags:','domain'), ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','domain'); ?><?php the_time(get_option('date_format')); ?><br/>
            <?php if ( comments_open() ) {
            comments_popup_link(__('No Comments &#187;','domain'), __('1 Comment &#187;','domain'), __('% Comments &#187;','domain'));} 
            else {
            _e('Comments are disabled!','domain');
            }
            ?>
            <?php edit_post_link(__('&nbsp;Edit','domain'), __('&nbsp;&#124;','domain'), ''); ?>
            </p>
        </div><!--/entry-->
    </div><!--/post_class-->

    <?php endwhile; ?>

<div class="clear"></div>   

<div class="navigation">

<?php

    global $wp_query;

    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages,
    'prev_text' => __('Prev','domain'),    
    'next_text' => __('Next','domain')   
    ) );                                    
 ?>

</div><!--/Navigation-->

    <?php endif; ?><!--END if THE LOOP (3 Posts)-->

<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
于 2013-10-23T20:47:39.860 回答