Support » Fixing WordPress » Highlighting current post title in get_posts list

  • Hi

    Can anyone help?

    I’ve got the following code in an include file on my single.php page to display the posts from the category ‘Archive’;

    <div id="newsArchivePanel">
      <h2><a href="/category/archive/">News Archive</a></h2>
      <ul>
        <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts( array ( 'category_name' => 'Archive', 'posts_per_page' => 3 ) );
    foreach($myposts as $post) :
    ?>
        <li <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'class="currentPost"' ?>><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a></li>
        <?php endforeach; ?>
      </ul>
    </div>

    However, I am also using the same code to highlight the post title in the ‘latest news category’.

    The post title highlighting works for the Latest News list, but not the archive list when viewing an archived post.

    Is there a way of combining these two sets of code at all to make them both work?

    Thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    This is also in single.php?
    try it with this:

    global $post;
    $IDOutsideLoop = $post->ID;
    
    $myposts = get_posts( array ( 'category_name' => 'Archive', 'posts_per_page' => 3 ) );
    foreach($myposts as $post) :
    setup_postdata($post);
    //rest of code ...

    Thanks for the code but that didn’t seem to work for me :S

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Is it also in the same single.php or are is this in some other theme template file (sidebar.php)?

    The code calling the ‘latest news’ post list is in sidebar.php which single.php is pulling in.

    The code calling the ‘archive’ post list is in an include file called inc_newsArchive.php which single.php is also pulling in.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    How are you including these templates? get_sidebar, get_template_part, include(), require()?

    Hi – I’m using the following code

    <?php get_sidebar(); ?>
    <?php require_once('includes/inc-newsArchive.php'); ?>

    🙂

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try replacing the one that isn’t working whit include or include_once

    include_once('sidebar.php');

    Hi – that didn’t seem to work either. Is there a way of combining both sets of code together?

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Can you paste and submit the full code of sidebar.php and inc-newsArchive.php into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Hi, of course.

    Sidebar.php file is as follows;

    <div id="archiveListPanel">
      <h2><a href="/category/latest-news/">Latest News</a></h2>
      <ul>
        <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts( array ( 'category_name' => 'Latest News', 'posts_per_page' => -1 ) );
    foreach($myposts as $post) :
    ?>
        <li <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'class="currentPost"' ?>><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a></li>
        <?php endforeach; ?>
      </ul>
      </div>
      <?php require_once('includes/inc-locationPanel.php'); ?>

    and the source of inc-newsArchive.php is

    <div id="newsArchivePanel">
      <h2><a href="/category/archive/">News Archive</a></h2>
      <ul>
        <?php
    
    $IDOutsideLoop = $post->ID;
    global $post;
    
    $myposts = get_posts( array ( 'category_name' => 'Archive', 'posts_per_page' => 3 ) );
    foreach($myposts as $post) :
    ?>
        <li><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a></li>
        <?php endforeach; ?>
      </ul>
    </div>

    Thank you for your help so far 🙂

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try changing this in inc-newsArchive.php.

    <li><a href="<?php the_permalink(); ?>">

    to this (as in sidebar.php):

    <li <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'class="currentPost"' ?>><a href="<?php the_permalink(); ?>">

    Hi – yes that’s how I had it originally and it just doesn’t work. The class is just not being added :S

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    I’ve tried both on my site and the class gets added.

    Can you paste and submit the full code of single.php into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Hi – sorry for the delayed response, here is my pastebin link – http://pastebin.com/M9vvUm7t

    Thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Highlighting current post title in get_posts list’ is closed to new replies.