我正在尝试制作一个Wordpress博客,其中主页只显示最近的3篇文章,但对于这些最新的文章,它会显示整个文章(不仅仅是摘要内容),然后对于其他2篇,它会使用“阅读更多”按钮显示摘要内容。
我目前正在以“基本”主题构建所有这些。
我对Wordpress和PHP还是相当陌生的,但在HTML/CSS方面有扎实的背景,还有一点Java背景。
有什么想法吗?
还有,有没有办法控制在“阅读更多”按钮之前显示多少帖子?
我正在尝试制作一个Wordpress博客,其中主页只显示最近的3篇文章,但对于这些最新的文章,它会显示整个文章(不仅仅是摘要内容),然后对于其他2篇,它会使用“阅读更多”按钮显示摘要内容。
我目前正在以“基本”主题构建所有这些。
我对Wordpress和PHP还是相当陌生的,但在HTML/CSS方面有扎实的背景,还有一点Java背景。
有什么想法吗?
还有,有没有办法控制在“阅读更多”按钮之前显示多少帖子?
向查询中添加计数器,并根据计数的不同更改输出。
您需要直接编辑参数,将每页的posts\\u限制为3个,或者使用pre\\u get\\u posts进行编辑。
pre-get-posts示例(到您的functions.php中)
function hwl_home_pagesize( $query ) {
if ( is_home() ) {
// Display only 3 post for the original blog archive
$query->set( \'posts_per_page\', 3 );
return;
}
}
add_action( \'pre_get_posts\', \'hwl_home_pagesize\', 1 );
然后进入你的家。php(或基本主题使用的任何文件):if ( have_posts() ) {
$i=1;
while ( have_posts() ) {
the_post();
//
if ($i==1) {
//first post
the_title();
the_content();
}else{
//other 2 posts
the_title();
the_excerpt();
}
//
$i++;
} // end while
} // end if
现在,在仪表板中,还要确保选择了设置/读取选项“显示全文”。或者,如果要启动自己的查询,可以不使用主查询:
// WP_Query arguments
$args = array( \'posts_per_page\' => \'3\'; \'post_type\' => \'posts\';
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
$i=1;
while ( $query->have_posts() ) {
$query->the_post();
// do something
if ($i==1) {
//first post
the_title();
the_content();
}else{
//other 2 posts
the_title();
the_excerpt();
}
$i++;
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
这将消除添加到函数的需要。php对于你问题的第一部分,我会用这个来实现它
// FIRST LOOP: display posts 1 query_posts(\'showposts=1\'); $posts = get_posts(\'numberposts=1&offset=0\'); foreach ($posts as $post) : start_wp(); static $count1 = 0; if ($count1 == "1") { break; } else {
the_title(); the_content();
$count1++; } endforeach;
// SECOND LOOP: display posts 2 and 3 query_posts(\'showposts=2\'); $posts = get_posts(\'numberposts=2&offset=1\'); foreach ($posts as $post) : start_wp(); static $count2 = 0; if ($count2 == "2") { break; } else {
the_title(); the_excerpt();
$count2++; } endforeach;
关于在阅读更多之前显示多少的问题的第二部分,请将其添加到函数文件中
function new_excerpt_length($length) { return 15; } add_filter(\'excerpt_length\', \'new_excerpt_length\');
将数字15更改为您希望显示的金额您最多可以从wordpress管理面板设置>阅读设置>博客页面显示中设置每页的帖子,设置为您需要的任何内容。
$args = array(
\'posts_per_page\' => 3,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
);
$posts_array = get_posts( $args );
echo "<ul>";
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content();?>
</li>
<?php endforeach;
wp_reset_postdata();?>
echo "</ul>";
我对粘贴帖子有问题。这是我的循环:<?php $main_loop = array( \'posts_per_page\' => 3, \'orderby\' => \'date\', ); $query = new WP_Query($main_loop); ?> <?php if ($query->have_posts()): ?> <?php while