admin管理员组文章数量:1391995
i am facing a issue, on my wordpress site in left side i loaded post list with title and excerpt, and when you click on that content will get load on right side of page and its working fine
but i like to add active or current class when any link from list get clicked and open in side, and first one as active already as it is already displaying content on right side
please check code
on left side i am using
<?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='/?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='/?postid=<?php echo $id; ?>#post'" class="blog-read-more" > READ MORE</a></p>
</div></div>
</article> <?php
endwhile;
?>
and on right side i am using
<?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>
now issue is that i am want to add current or active class to article like class= "et_blog_post active" if current post is opened in right side for that i tried lot of solution that are provided here, i like to use url==href it add class but nothing working please suggest a solution thanks in advance and sorry for bad english
i am facing a issue, on my wordpress site in left side i loaded post list with title and excerpt, and when you click on that content will get load on right side of page and its working fine
but i like to add active or current class when any link from list get clicked and open in side, and first one as active already as it is already displaying content on right side
please check code
on left side i am using
<?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/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/blog/?postid=<?php echo $id; ?>#post'" class="blog-read-more" > READ MORE</a></p>
</div></div>
</article> <?php
endwhile;
?>
and on right side i am using
<?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>
now issue is that i am want to add current or active class to article like class= "et_blog_post active" if current post is opened in right side for that i tried lot of solution that are provided here, i like to use url==href it add class but nothing working please suggest a solution thanks in advance and sorry for bad english
Share Improve this question asked Apr 1, 2020 at 7:38 arjun kushwaharjun kushwah 133 bronze badges1 Answer
Reset to default 0For the left side you could do this,
<?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();
And instead of setting the excerpt length with substr()
, you can use excerpt_length
filter. In the example above I added custom action which adds the length filter so that it happens only in this context and not site wide.
// 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;
}
And for completeness sake, example for the right side,
<?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 queryadd active class based on permalink and url
版权声明:本文标题:wp query - add active class based on permalink and url 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744602484a2615150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论