2017-07-17 63 views
-1

我目前正在处理我的第一个WordPress主题,并且想要在index.php上创建“突出显示的帖子”部分。这里有很多脚本,但似乎都在整个index.php上应用永久过滤器。这是有问题的,因为我想在我的index.php下面显示整个帖子。有没有办法将这个特殊的过滤器只适用于这个部分,而不是整个index.php?仅在首页显示WordPress帖子的第一句话作为“突出显示的帖子”部分

+1

我建议你为此使用JavaScript/jQuery – tamal3053

回答

1

如果你可以给我们更多的信息什么决定了这些“突出显示”的帖子,我们可能会更具体地解决这个问题。

但在平均时间,我会想这是两款最新的帖子,这意味着你可以有两个查询在index.php

第一个将是“突出”的:

<?php 
    $args = array('numberposts' => 2); 
    $lastposts = get_posts($args); 
    foreach($lastposts as $post) : setup_postdata($post); 
?> 

    <h2 class="news"><?php the_title(); ?></h2> 
    <?php the_excerpt(); ?> 

<?php endforeach; ?> 

这会给你在这个查询中的文章的摘录。

你可以做的,然后是限制摘录自己喜欢的任意长度和添加,如果你想:)

function newExcerpt($more) { 
    global $post; 
    return '... <a href="'. get_permalink($post->ID) . '">Read more</a>'; 
} 
add_filter('excerpt_more', 'newExcerptReadMore'); 


function customExcerptLength($length) { 
    return 20; 
} 
add_filter('excerpt_length', 'customExcerptLength', 999); 

读更多链接的其他职位,你可以做另一个查询,但不使用前两个这样的帖子:

$count = 0; 
$lastposts = get_posts(); 
foreach($lastposts as $post) : setup_postdata($post); 
if($count > 1) 
?> 

    <h2 class="news"><?php the_title(); ?></h2> 
    <?php the_excerpt(); ?> 
    $counter++; 

<?php endforeach; ?> 

这将只是循环通过帖子和跳过前两个帖子。 如果您有其他一些术语或类别或其他决定突出显示的帖子的内容,那么您可以使用它并将其作为参数传递给get_posts($ args)。

+0

这是我想要的,非常感谢你!目前我没有时间来测试它,但如果出现错误,我宁愿再次回答这个问题。 :^) – F9lke

+0

没问题让我知道它是如何去的:)并接受答案,如果这对你有好处:) –

+0

好吧,现在我准备好测试它了! :^)所以关于你的第二个帖子..我必须在哪里定位它?在functions.php中?但是第三行代码呢?先谢谢你! – F9lke