根据固定链接和URL添加活动类

时间:2020-04-01 作者:arjun kushwah

我面临一个问题,在我左侧的wordpress网站上,我加载了带有标题和摘录的帖子列表,当你们点击该内容时,页面右侧的内容就会加载,并且工作正常

但我喜欢在列表中的任何链接被单击并在侧边打开时添加active或current类,第一个类已经是active类了,因为它已经在右侧显示了内容

请检查代码

在左侧,我正在使用

<?php
 $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1; 

$args = array(
\'post_type\' => \'post\',  //Specyfying post type    
\'posts_per_page\' => 10,  //No. of posts to show     
\'paged\' => $paged       //For pagination
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
    //To get ID of post
    $id = get_the_ID();
    $excerpt = get_the_excerpt();
    $excerpt = substr( $excerpt , 0, 100);

    ?>  
<article id="post-<?php the_ID(); ?>" class="et_blog_post"  >
<div class="entry-content">   
   <h3 calss="entry-title" >  
       <a onclick="location.href=\'https://example.com/blog/?postid=<?php echo $id; ?>#post\'"  ><?php the_title(); ?></a></h3>
<div class="post-content"><div class="post-content-inner">
<p><?php echo $excerpt; ?></p></div>
<p><a onclick="location.href=\'https://example.com/blog/?postid=<?php echo $id; ?>#post\'"  class="blog-read-more"  > READ MORE</a></p>

     </div></div>
</article> <?php
endwhile;
?>
在右侧,我使用

<?php

    $post = $_GET[\'postid\'];  //Fetching value of postid from url
    //To show post\'s content
        $include = get_posts("include=$post");
        $title = get_the_title();
      $content = apply_filters(\'the_content\',$include[0]->post_content);?>
<div class="entry-content">   
   <h1><?php echo get_the_title($post); ?></a></h1>
     <p><?php echo $content; ?></p>
现在的问题是,我想在文章中添加当前或活动类,如class=“et\\u blog\\u post active”如果当前文章在右侧打开,我尝试了这里提供的许多解决方案,我喜欢使用url==href it add class,但没有任何效果,请提出一个解决方案,提前谢谢,抱歉英语不好

1 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

对于左侧,你可以这样做,

<?php
$args = array(
  \'post_type\'      => \'post\',
  \'posts_per_page\' => 10,   
  \'paged\'          => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1,
);
$loop = new WP_Query( $args );

// add custom function before loop, so you can apply excerpt filter only to this case
do_action(\'my_custom_pre_loop_action\');

// is url parameter set, if so force it to integer
$current_post_id = ! empty( $_GET[\'postid\'] ) ? absint($_GET[\'postid\']) : 0;

while ( $loop->have_posts() ) : $loop->the_post();
  $post_class = \'et_blog_post\';
  // is the current loop item the same as the post in view?
  if ( get_the_id() === $current_post_id ) {
    $post_class .= \' active\';
  } else if ( 0 === $loop->current_post ) {
    $post_class .= \' active\' // make first post active as a fallback
  }
?>
  <article id="post-<?php the_ID(); ?>" class="<?php echo $post_class ?>">
    <div class="entry-content">   
      <h3 class="entry-title">  
        <a href="/blog/?postid=<?php the_ID(); ?>#post">
          <?php the_title(); ?>
        </a>
      </h3>
      <div class="post-content">
        <div class="post-content-inner">
          <?php the_excerpt(); ?>
        </div>
        <p>
          <a href="/blog/?postid=<?php the_ID(); ?>#post" class="blog-read-more">READ MORE</a>
        </p>  
      </div>
    </div>
  </article>
<?php 
endwhile;
// reset global $post after custom WP_Query loop
wp_reset_postdata();
而不是将摘录长度设置为substr(), 您可以使用excerpt_length 滤器在上面的示例中,我添加了自定义操作,该操作添加了长度过滤器,以便它仅在该上下文中发生,而不是在站点范围内发生。

// apply custom exerpt lenght only when your custom action is fired
add_action(\'my_custom_pre_loop_action\', \'apply_my_custom_exerpt_length\');
function apply_my_custom_exerpt_length() {
  // if you want to apply this globally on your site then just move the add_filter to your functions.php
  add_filter(\'excerpt_length\', \'my_custom_exerpt_length\');
}

function my_custom_exerpt_length($length) {
  return 100;
}
为了完整起见,比如右侧,

<?php
$current_post_id = ! empty( $_GET[\'postid\'] ) ? absint($_GET[\'postid\']) : 0;
$post = get_post( $current_post_id );
?>
<div class="entry-content">
  <?php if ( $post ) : ?>
    <h1><?php echo get_the_title($post); ?></h1>
    <?php echo apply_filters( \'the_content\', get_the_content($post) ); ?>
  <?php endif; ?>
</div>

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post