簡體   English   中英

在wordpress上設置類別頁面中的自定義帖子數

[英]On wordpress set a custom number of posts in a category page

我有一個wordpress網站,在類別10的自定義模板中,我有以下代碼:

<?php
function new_excerpt_length($length) {return 30;} add_filter('excerpt_length', 'new_excerpt_length');
global $post;
$args = array( 'numberposts' => 5, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
echo get_the_post_thumbnail($post_id, $size)?></div><div id="eu_post_category">
<h2><a class="roll-link" href="<?php the_permalink(); ?>"><span data-title="<?php the_title(); ?>"><?php the_title(); ?></span></a></h2>
<h6><?php the_excerpt(); ?></h6> 
<?php endforeach; ?>

這將返回我類別中的最后五個帖子。 但是我不想失去以前的職位。 我想在底部有兩個按鈕,較新的帖子||較舊的帖子,以便讓用戶看到所有帖子。

我試過的:在settings->讀取中設置最大數量,並在functions.php中使用此代碼(無結果):

function limit_posts_per_archive_page() {
if ( is_category('10') )
set_query_var('posts_per_archive_page', 5); // or use variable key: posts_per_page
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');

謝謝!

<當您單擊新帖子/舊帖子按鈕時,獲取頁碼(存儲在$ paged變量中)在參數內傳遞此頁碼

     <?php
        $args = array(
            'paged'            => $paged,
            'category'        => 10,
            'posts_per_page'  => 5,
            'post_status'     => 'publish');
        query_posts($args);
        if ( have_posts() ) {
            while ( have_posts() ) { the_post();
                echo $post->post_title;
            }

       }
   ?>

供參考: http : //codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Template_Tags/get_posts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM