【问题标题】:Show posts in WordPress在 WordPress 中显示帖子
【发布时间】:2019-07-18 03:12:58
【问题描述】:

我是 WordPress 的新手,正在尝试对网站进行更改。

现在该网站显示无限的帖子和文章。我想将其更改为仅一起显示 5 篇文章和帖子。

我不知道如何更改它,因为它也连接到按钮。当您单击此按钮时,它仍会显示所有帖子和文章。

有人知道我该如何继续前进吗?

最好的问候,

<?php get_header();?>

	<?php get_template_part( 'templates/top', 'section' ); ?>

	<section class="blog-section">

		<div class="row">

			<?php get_sidebar( 'blog' ); ?>
	
		    <main id="main" class="blog-section__main small-12 large-8 medium-6 small-order-1 medium-order-2 columns" role="main">
		    
				<div class="news-feed">
					<h2 class="news-feed__heading h3"><?php echo __('Articles and blog posts'); ?></h2>
				    <?php
				    
				    // Fetch news posts
				    $posts = get_news_posts([
				    	'posts_per_page' => NEWS_POSTS_PER_PAGE
				    ]);

				    if ($posts->have_posts()) : while ($posts->have_posts()) : $posts->the_post();

						get_template_part( 'templates/loop', 'news' );
					    
					endwhile;
					wp_reset_postdata();
					?>
				</div>
				
				<?php
				if( $posts->found_posts > $posts->post_count )
					echo '<a href="#" data-offset="' .  NEWS_POSTS_PER_PAGE . '" id="load-more-news" class="button button--primary"> <div class="news-button">' . __('Load more') . '</div> </button>'
				?>

<!-- 					<?php page_navi(); ?> -->
					
				<?php else : ?>
											
					<?php get_template_part( 'templates/content', 'missing' ); ?>
						
				<?php endif; ?>
																								
		    </main> <!-- end #main -->

		</div> <!-- end .row -->

	</section> <!-- end .blog -->

	<?php 
		
		global $post;

		$qo = get_queried_object();

		$post->ID = $qo->ID;

		get_template_part('templates/flexible-content'); 

	?>

<?php get_footer(); ?>

【问题讨论】:

    标签: wordpress loops posts


    【解决方案1】:

    posts_per_page 参数设置要返回的帖子数。在您的代码中,这被设置为常量NEWS_POSTS_PER_PAGE

    $posts = get_news_posts([
        'posts_per_page' => NEWS_POSTS_PER_PAGE
    ]);
    

    get_news_posts 不是原生 WP 功能,所以我假设它在您正在使用的自定义主题或插件中,并且可能主题/插件具有更改 NEWS_POSTS_PER_PAGE 值的设置,所以您真的不应该不要更改代码。

    更改此设置的正确方法是查找并更改该设置,以便将其应用于插件的每次使用。但是,如果您真的需要在代码中执行此操作,您可以为 posts_per_page 设置不同的值,例如

    $posts = get_news_posts([
        'posts_per_page' => 5
    ]);
    

    但是直接改代码可能会导致其他问题,如果不是你自己的主题,那么你更新的时候修改会被覆盖,所以请先在主题或插件中寻找设置!强>

    【讨论】:

    • 非常感谢,现在我明白了。我会在主题中寻找设置或检查是否使用了插件。
    • @Erhan 很高兴听到它 :) 但至少现在您知道在哪里设置了值,如果您必须自己更改代码的剧烈行动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多