繁体   English   中英

帖子类型:当前帖子在帖子列表中选中

[英]Post Type: Current Post Checked in the list of posts

我在WP中有一个职位类型,并且有一个带有以下职位列表的菜单:

<ul class="menu2">  
        <?php
        $query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );  
                while ( $query->have_posts() ) : $query->the_post();
                    echo '<li ';
                    echo '><a href="';
                    the_permalink();
                    echo '">';
                    the_title();
                    echo '</a></li>';
                endwhile;?>
    </ul>

我想知道如何选择当前帖子以添加“ class =“ selected””之类的文本,以黑色或类似的形式显示出来。

谢谢!

<ul class="menu2">  
        <?php
        $postId = get_the_ID();
        $query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );  
                while ( $query->have_posts() ) : $query->the_post();
                   $idPost = get_the_ID();                
                    echo '<li ';
                    if( $idPost==$postId)  echo 'class="selected"';
                    echo '><a href="';
                    the_permalink();
                    echo '">';
                    the_title();
                    echo '</a></li>';
                endwhile;?>
    </ul>

它是single-post.php模板中的侧边栏菜单(在这种情况下,archive-post模板是不相关的)。

这里是完整的代码:

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="main">
    <div class="boxCenter">
        <div class="content"><!-- CONTENT -->
            <div class="mainContent">
                <div class="entry">
                    <div class="textBoxSmall">
                        <div class="comisario">
                            <img src="<?php the_field('imagen_del_comisario'); ?>">
                        </div>
                        <div class="textSmall comisarioText">
                            <h1><?php echo get_the_title(); ?></h1>
                            <p><?php the_field('res_comisario'); ?></p>      
                            <a href="http://<?php the_field('web_del_comisario'); ?>"><?php the_field('web_del_comisario'); ?></a> 
                            </p>

                            <p>Exposiciones:</p>
                            <div class="exposiciones">
                                <?php while(have_rows('exposiciones_del_comisario')): the_row(); ?>
                                    <?php 
                                        $contador++;
                                    ?>
                            <div class="square box<?php echo $contador; ?>">
                                <div class="center">
                                    <a href="<?php the_sub_field('exposicion'); ?>">
                                        <?php echo $contador; ?>
                                    </a>
                                </div>
                            </div>
                            <?php endwhile; ?>  
                        </div>  
                    </div>
                </div>


                <div class="textBox">

                  <?php the_content();?>

            </div>    
        </div>
    </div>      
</div><!-- CONTENT END -->
<?php endwhile; // end of the loop. ?>   


<div class="sidebar hide"><!-- LATERAL MENU -->
    <ul class="menu2">  
        <?php
        $query = new WP_Query( array( 'post_type' => array( 'comisarios' ) ) );  
                while ( $query->have_posts() ) : $query->the_post();
                    echo '<li ';
                    echo '><a href="';
                    the_permalink();
                    echo '">';
                    the_title();
                    echo '</a></li>';
                endwhile;?>
    </ul>

</div><!-- END LATERAL MENU -->  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM