2014-09-18 72 views
0

我有一个自定义页面模板设置为将类别文章拉到“页面”上 我已经为摘录显示设置了一个函数。 “阅读更多>>”存在,但是,帖子ID并未吸引单个帖子的固定链接。每篇文章后的摘录链接到我创建的同一页面。节选“阅读更多>>”指向当前网址

的functions.php - >

function excerpt($num) { 
$limit = $num+1; 
$excerpt = explode(' ', get_the_excerpt(), $limit); 
array_pop($excerpt); 
$excerpt = implode(" ",$excerpt). ' <a href="' . get_permalink($post->ID) . '" class="more-link" title="Read More">Read More >></a>'; 
echo $excerpt; 
} 

function excerpt_length($length) { 
    return 40; 
} 
add_filter('excerpt_length', 'excerpt_length'); 

content.php - >

<div class="entry-content"> 
<?php if (is_category() || is_archive() || is_search() || is_home()) { 
     the_excerpt(); 
    } else { 
     the_excerpt(); 
    } ?> 
</div> 

-------另一个功能我已经具有相似的结果进行测试时,其包括全球$后。

function themprefix_excerpt_read_more_link($output) { 
    global $post; 
return $output . ' <a href="' . get_permalink($post->ID) . '" class="more-link" title="Read More">Read More >></a>'; 
} 
add_filter('the_excerpt', 'themprefix_excerpt_read_more_link'); 

自定义页面模板 - >

<?php get_header(); ?> 
    <div id="main" class="row-fluid"> 
    <div id="main-left" class="span8"> 
     <?php while (have_posts()) : the_post(); ?> 
     <?php get_template_part('content', 'page'); ?> 

      <?php 
       $catPost = get_posts('cat=225&posts_per_page=3'); 
       foreach ($catPost as $post) : setup_postdata($post); 
      ?> 

     <?php get_template_part('content'); ?> 
     <?php endforeach;?> 
     <?php comments_template('', true); ?> 
     <?php endwhile; // end of the loop. ?> 
    </div><!-- #main-left -->  
     <?php get_sidebar(); ?> 
    <div class="clearfix"></div> 
    </div><!-- #main --> 
<?php get_footer(); ?> 

回答

2

尝试在你的excerpt()函数开始使用global $post

+0

它通常更喜欢将函数解析为函数,而不是使其成为全局'函数摘要($ num,$ post)' – 2014-09-18 21:15:43

+0

你是对的,但这取决于你如何使用函数,以及你有什么控制超过其参数。总体而言,如果您无法控制参数,并且您正在使用主查询,则最简单的方法是全局化$ post变量。 – 2014-09-18 21:17:17

+0

我一直无法使用任何建议产生结果 – Visitor14 2014-09-18 22:22:47