Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示文章类型的所有标题,并在重定向时突出显示当前页面_Php_Wordpress - Fatal编程技术网

Php 显示文章类型的所有标题,并在重定向时突出显示当前页面

Php 显示文章类型的所有标题,并在重定向时突出显示当前页面,php,wordpress,Php,Wordpress,这是我的代码,它的工作是从post类型中调用标题。因此,对于这个示例,它调用的标题如下 标题一 标题二 标题二 这个列表也是链接,如果我点击“Title II”,它会重定向到右边的单页,但我的问题是它没有突出显示当前的单页。换句话说,我想让它看起来像这样: 标题一 标题二 标题二 但我的代码结果如下: 标题一 标题二 标题二 下面是我页面中的代码,这也是我单个页面中的代码 <?php $args = array( 'post_type' => 'services'

这是我的代码,它的工作是从post类型中调用标题。因此,对于这个示例,它调用的标题如下

标题一
标题二
标题二

这个列表也是链接,如果我点击“Title II”,它会重定向到右边的单页,但我的问题是它没有突出显示当前的单页。换句话说,我想让它看起来像这样:

标题一
标题二
标题二

但我的代码结果如下:
标题一
标题二
标题二

下面是我页面中的代码,这也是我单个页面中的代码

<?php $args = array(  
        'post_type' => 'services',
        'posts_per_page' => -1
    );
             $the_query = new WP_Query( $args );?>
            <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <li>
                    <a href="<?php the_permalink()?>"><?php echo the_title(); ?></a>
                </li>
            <?php endwhile?>
            <?php endif; wp_reset_postdata();?>


  • 您需要进行测试,查看每篇文章的链接是否与当前页面URL匹配


    您需要进行测试,查看每篇文章的链接是否与当前页面URL匹配


    您可能希望使用ID进行比较。请记住在循环之前获取当前ID

    <?php 
    // Remember to get ID before the loop to have current ID
    $current_post_ID = get_the_ID();
    $args = array(  
        'post_type' => 'services',
        'posts_per_page' => -1
    );
    
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a <?php echo $current_post_ID === get_the_ID() ? 'class="active"' : '' ?> href="<?php the_permalink()?>"><?php echo the_title(); ?></a>
        </li>
    <?php endwhile?>
    <?php endif; wp_reset_postdata();?>
    
    
    

  • 您可能希望使用ID进行比较。请记住在循环之前获取当前ID

    <?php 
    // Remember to get ID before the loop to have current ID
    $current_post_ID = get_the_ID();
    $args = array(  
        'post_type' => 'services',
        'posts_per_page' => -1
    );
    
    $the_query = new WP_Query( $args );
    ?>
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a <?php echo $current_post_ID === get_the_ID() ? 'class="active"' : '' ?> href="<?php the_permalink()?>"><?php echo the_title(); ?></a>
        </li>
    <?php endwhile?>
    <?php endif; wp_reset_postdata();?>