【问题标题】:Excerpt "Read more>>" pointing to current URL摘录“阅读更多>>”指向当前 URL
【发布时间】:2014-09-18 21:07:47
【问题描述】:

我有一个自定义页面模板设置为将类别帖子拉到“页面”上 我已经设置了一个功能来显示摘录。 “阅读更多>>”是存在的,但是,帖子 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>

-------我一直在测试的另一个功能类似的结果,其中包括全局 $post。

 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(); ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    【讨论】:

    • 通常最好将 var 解析到函数中,而不是使其成为全局 function excerpt($num,$post)
    • 你是对的,但这取决于你如何使用这个函数,以及你对它的参数有什么控制。总的来说,如果您无法控制参数,并且您正在使用主查询,那么最简单的方法是全局化 $post 变量。
    • 我无法使用任何一个建议产生结果
    • 你能再贴一些代码吗?您在哪里以及如何使用 excerpt() 函数?
    • 我还是没看到 excerpt() 函数在哪里被调用。
    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2017-02-10
    • 2013-06-13
    相关资源
    最近更新 更多