WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlighting current post title in get_posts list (14 posts)

  1. heriz
    Member
    Posted 5 years ago #

    Hello.

    In my sidebar, on my single template, I'm listing all posts under a given category. Code:

    <ul>
    	 <?php
    	 global $post;
    	 $myposts = get_posts('showposts=30&category=175');
    	 foreach($myposts as $post) :
    	 ?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	 <?php endforeach; ?>
    </ul>

    Does anyone have an idea of how to make the current post title highlighted using the style ('current-post-item'/'current-page-item' etc) allocated to it?

    I've been looking at using something similar to

    <li<?php
    if (is_home())
    {
    echo " class=\"current_page_item\"";
    }
    ?>>

    But I can't find an 'is_ ' function for the current post title.

    Any help much appreciated!

  2. ahmad87
    Member
    Posted 5 years ago #

    You can use something like this

    <ul>
    <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts('showposts=5');
    foreach($myposts as $post) :
    ?>
    
    <li <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'style="font-weight:bold";' ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    </ul>

    Simply adds a statement that will print bold style if the ID outside loop matches the ID inside loop for single post.

  3. heriz
    Member
    Posted 5 years ago #

    ahmad - that worked a treat.

    thank you so much!

  4. petah2014
    Member
    Posted 5 years ago #

    ahmad you rock. Here's the version with a class instead of print

    <ul class="sidebar-ul">
    
    <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts('showposts=5');
    foreach($myposts as $post) :
    ?>
    
    <li <?php
    if(is_single() && $IDOutsideLoop == $post->ID) 
    
    {
    echo " class=\"current\"";
    }
    ?>>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endforeach; ?>
    
    </ul>

    of course set your own css

  5. macmarty
    Member
    Posted 5 years ago #

    Hello, the above code is working nicely if you have one list of post.
    But it stop highlighting the current post if you use it to have more than one list.

    Let's say i need to show
    > Category 1
    >>Post3
    >>Post5

    > Category 2
    >>Post1
    >>Post2
    >>Post4

    How can i tweak the code to make it work?

    I suppose it's something related to the

    $IDOutsideLoop = $post->ID;
    global $post;

    code, but i don't know hot to solve it.

  6. melzor
    Member
    Posted 5 years ago #

    I've been trying to do something similar to this, but only view post titles from the current category you are viewing. I have another code snippet which does this, although, it doesn't have the capability to highlight the currently viewed post.

    <ul>
    	<?php global $post;
    	$categories = get_the_category();
    	foreach ($categories as $category) :
    	?>
    	<h3>More News From This Category</h3>
    	<ul>
    	<?php
    	$posts = get_posts('numberposts=20&amp;category='. $category->term_id);
    	foreach($posts as $post) :
    	?>
    	<li <?php if ( is_category('4') ) { ?> class="current_page_item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    
    	<?php endforeach; ?>
    </ul>

    I've tried merging both together but without success. Anyone know how?

  7. melzor
    Member
    Posted 5 years ago #

    The code posted here displays posts ONLY from the current category. If only we could merge both together! I don't know PHP well enough to do this. Anyone?

    <ul>
    
    <?php while(have_posts()) : the_post(); ?>
    
    <?php foreach((get_the_category()) as $category)
    { $my_query = new WP_Query('category_name=' . $category->category_nicename . '&amp;orderby=title&amp;order=asc&amp;showposts=100');} ?> 
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    
    <?php break; endwhile; ?>
    
    </ul>
  8. eggnog
    Member
    Posted 5 years ago #

    Brilliant! Thanks for the code. You saved my ass.

  9. deWINTER
    Member
    Posted 5 years ago #

    Displaying the post from the current category and highlighting the current post - combines both solutions.

    <ul>
          		<?php foreach((get_the_category()) as $category) { ?>
    				<h3><?php echo $category->category_description . ' '; ?></h3>
                    <?php $catVal = $category->cat_ID; }
    	  		$IDOutsideLoop = $post->ID;
    	  		global $post;
    	  		$myposts = get_posts('category='.$catVal);
    			foreach ($myposts as $post) { ?>
    				<li<?php if($IDOutsideLoop == $post->ID) { echo " class=\"current\""; } ?>>
                    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php }; ?>
           	</ul>
  10. cornhustlah
    Member
    Posted 5 years ago #

    ^wanna take this a step further and set it so that the categories get displayed all at once together with the lists of posts set under their respective categories (and between the other cats) so that this appears as a list of chapters and subjects? on top of that it might be nice if the posts appeared below their cats only when that cat is active.

    im trying to hack it up myself but am ultra n00b at PHP/Wordpress...

  11. cornhustlah
    Member
    Posted 4 years ago #

    ok, so it took me a while but here is the code - which i put in my sidebar.php - to list all available categories AND list each category's posts under their respective categories. the code i've pieced together also highlights the current post AND current category by adding a CSS class to each using if statements. coupled with CSS i've used this to create an accordion-like nav system of said categories and posts:

    <?php if (is_home(''))
    {
    	wp_list_categories('title_li=&orderby=ID');
    }
    else
    {
    	wp_reset_query();
    	$catVal = get_the_category();
    	$myCat = $catVal[0]->cat_ID;
    
    	$IDOutsideLoop = $post->ID;
    	global $post;
    	?>
    	<?php $categories = get_categories('child_of'.$myCat.'&orderby=ID&order=asc'); /// Parent Category number
    	foreach ($categories as $cat)
    	{
    		$var = $cat->cat_ID ; $lastposts = get_posts('category='.$var.'&orderby=date&order=dsc');
    		wp_list_categories('include='.$cat->cat_ID.'&title_li=');?><?php // echo $cat->cat_name. " (" .count($lastposts).")";?>
    		<ul class="toggle_content<?php if($myCat == $cat->cat_ID) { echo ' current-category'; } ?>">
    		<?php foreach($lastposts as $post)
    		{
    			setup_postdata($post); ?>
    			<li<?php if($IDOutsideLoop == $post->ID) { echo ' class="current-post"'; } ?>><a <?php post_class();?> href="<?=the_permalink()?>"><?=the_title()?><span></span></a></li>
    			<?php
    		} ?>
    		</ul>
    		<?php
    		}
    }?>
  12. deWINTER
    Member
    Posted 4 years ago #

    Cornhustlah, you're a star!
    It comes in handy on a custom build site map page as well.
    Thank you very much,

    Jo.

  13. fatanimalclub
    Member
    Posted 4 years ago #

    Hey guys,

    Currently going through all of this code and am at a complete loss. I've used the code that deWINTER posted up so that I can put a "current" class on my posts which I'm using as sidebar navigation.

    When I plunk the code into my sidebar.php it populates with a list of my category + posts that go under it... however the "current" class never gets put in when I'm on that page/post.

    However if I put the code in my header.php, the code works! I threw in a quick "li.current" into my css and it styled the page/post as it should!

    Any thoughts on what might be happening?

  14. deWINTER
    Member
    Posted 4 years ago #

    Hi,

    I placed the code in my single.php file in the sidebar div instead of calling the sidebar.php file (I use that for different page templates).
    Would that help?

    Jo.

Topic Closed

This topic has been closed to new replies.

About this Topic