如何将当前班级添加到当前帖子中?

时间:2011-08-25 作者:Jason

我列出了最新的7篇帖子:

            <ul id="post-list">
            <?php
            global $post;
            $args = array( \'numberposts\' => 7 );
            $myposts = get_posts( $args );
            foreach( $myposts as $post ) :  setup_postdata($post); ?>
                <li>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><span><?php the_author(); ?></span></a>
                </li>
            <?php endforeach; ?>
            </ul>
如何为当前帖子显示“current”类?

1 个回复
SO网友:Bainternet

try this:

<ul id="post-list">
    <?php
        global $post,$wp_query;
    $current_id = $wp_query->get_queried_object_id();
    $args = array( \'numberposts\' => 7 );
    $myposts = get_posts( $args );
        foreach( $myposts as $post ) :  setup_postdata($post); ?>
            <li<?php if ($current_id == $post->ID) echo \' class="current"\'; ?>>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><span><?php the_author(); ?></span></a>
                </li>   
    <?php endforeach; ?>
</ul>
结束

相关推荐

hooks & filters and variables

我是updating the codex page example for action hooks, 在游戏中完成一些可重用的功能(最初是针对这里的一些Q@WA)。但后来我遇到了一个以前没有意识到的问题:在挂接到一个函数以修改变量的输出后,我再也无法决定是要回显输出还是只返回它。The Problem: 我可以修改传递给do_action 用回调函数钩住。使用变量修改/添加的所有内容仅在回调函数中可用,但在do_action 在原始函数内部调用。很高兴:我将其修改为一个工作示例,因此您可以将其复制/粘贴