Php 如果当前页面与wordpress中的li名称匹配,则回显类

Php 如果当前页面与wordpress中的li名称匹配,则回显类,php,wordpress,class,if-statement,echo,Php,Wordpress,Class,If Statement,Echo,我试图找到一些逻辑,只有当当前页面标题与li中的标题匹配时,才会响应类。我很难想出一个不适用于此查询中所有li的“if”语句 这是wordpress 以下是上下文: <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>" <?php if(NEED LOGIC HERE) echo 'clas

我试图找到一些逻辑,只有当当前页面标题与li中的标题匹配时,才会响应类。我很难想出一个不适用于此查询中所有li的“if”语句

这是wordpress

以下是上下文:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   <li><a href="<?php the_permalink(); ?>" <?php if(NEED LOGIC HERE) echo 'class="current"'; ?>><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); endif; ?>

  • 任何帮助都将不胜感激

    如果您知道自己在某种类型的
    单张
    (即页面、帖子或类似内容),那么我想您可以在自定义循环之前保存该单张的ID,并与之进行比较:

    global $post;
    //Make sure you are on a single, otherwise you'll get funny results
    if (is_single()) {
        $saved_id = $post->ID;
    } else {
        $saved_id = false;
    }
    
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            echo '<li><a href="' . get_the_permalink() . '"';
            if($saved_id === $post->ID) {
                echo 'class="current"';
            }
            echo '>';
            the_title();
            echo '</a></li>';
        }
        wp_reset_query();
     }
    
    global$post;
    //确保你是单身,否则你会得到有趣的结果
    if(is_single()){
    $saved_id=$post->id;
    }否则{
    $saved_id=false;
    }
    if(have_posts()){
    while(have_posts()){
    _post();
    回音“
  • ”; } wp_reset_query(); }
    …很抱歉完全更改了代码样式,我无法使用模板标记使其可读

    哦,当然你可以比较标题,如果这确实更可取的话(虽然显然不那么精确),只要使用
    获取标题()
    $post->post\u title
    而不是
    $post ID

    ,如果你知道你是在某种类型的
    单张
    上(例如,一页、一篇文章或类似的),然后,我想您可以在自定义循环之前保存该循环的ID,并与该循环进行比较:

    global $post;
    //Make sure you are on a single, otherwise you'll get funny results
    if (is_single()) {
        $saved_id = $post->ID;
    } else {
        $saved_id = false;
    }
    
    if (have_posts()) {
        while (have_posts()) {
            the_post();
            echo '<li><a href="' . get_the_permalink() . '"';
            if($saved_id === $post->ID) {
                echo 'class="current"';
            }
            echo '>';
            the_title();
            echo '</a></li>';
        }
        wp_reset_query();
     }
    
    global$post;
    //确保你是单身,否则你会得到有趣的结果
    if(is_single()){
    $saved_id=$post->id;
    }否则{
    $saved_id=false;
    }
    if(have_posts()){
    while(have_posts()){
    _post();
    回音“
  • ”; } wp_reset_query(); }
    …很抱歉完全更改了代码样式,我无法使用模板标记使其可读


    哦,当然你可以比较标题,如果这确实更可取的话(虽然显然不那么精确),只需使用
    获取标题()
    $post->post\u title
    而不是
    $post ID

    好的,我想出来了。我需要跳出循环,创建一个有效的if语句来读取查询和当前页面中的链接。注意$titleID

    <?php
        $titleID = (get_the_title());
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $parentID = (wp_get_post_parent_id( $post_ID ));
        $args= array(
            'posts_per_page' => -1,
            'post_type' => 'page',
            'post_parent' => $parentID,
            'paged' => $paged
            );
            query_posts($args);
        ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
            <li><a href="<?php the_permalink(); ?>" <?php if(get_the_title() == $titleID ) echo 'class="current"'; ?>><?php the_title(); ?></a></li>            
        <?php endwhile; wp_reset_query(); endif; ?>
    
    
    

  • 好的,我想出来了。我需要跳出循环,创建一个有效的if语句来读取查询和当前页面中的链接。注意$titleID

    <?php
        $titleID = (get_the_title());
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $parentID = (wp_get_post_parent_id( $post_ID ));
        $args= array(
            'posts_per_page' => -1,
            'post_type' => 'page',
            'post_parent' => $parentID,
            'paged' => $paged
            );
            query_posts($args);
        ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
            <li><a href="<?php the_permalink(); ?>" <?php if(get_the_title() == $titleID ) echo 'class="current"'; ?>><?php the_title(); ?></a></li>            
        <?php endwhile; wp_reset_query(); endif; ?>
    
    
    

  • 您在哪里使用这个?使用一个新的方法可能更相关吗-然后会“自动”添加很多有用的类……是的,在大多数情况下导航菜单可能会这样做,但我正在为其他一些自定义函数从头开始构建它。wp_nav往往会妨碍某些定制。您到底在哪里使用它?使用一个新的方法可能更相关吗-然后会“自动”添加很多有用的类……是的,在大多数情况下导航菜单可能会这样做,但我正在为其他一些自定义函数从头开始构建它。wp_导航往往会妨碍某些定制。是的,这也很有效。在下面的答案中,我做了一些相似但不同的事情。谢谢你的帮助!是的,这也行。在下面的答案中,我做了一些相似但不同的事情。谢谢你的帮助!