WordPress.org

Ready to get started?Download WordPress

Forums

Highlighting current post title in get_posts list (15 posts)

  1. vikkineal
    Member
    Posted 2 years ago #

    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

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    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 ...
  3. vikkineal
    Member
    Posted 2 years ago #

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

  4. keesiemeijer
    moderator
    Posted 2 years ago #

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

  5. vikkineal
    Member
    Posted 2 years ago #

    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.

  6. keesiemeijer
    moderator
    Posted 2 years ago #

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

  7. vikkineal
    Member
    Posted 2 years ago #

    Hi - I'm using the following code

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

    :)

  8. keesiemeijer
    moderator
    Posted 2 years ago #

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

    include_once('sidebar.php');
  9. vikkineal
    Member
    Posted 2 years ago #

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

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  11. vikkineal
    Member
    Posted 2 years ago #

    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 :)

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    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(); ?>">
  13. vikkineal
    Member
    Posted 2 years ago #

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

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  15. vikkineal
    Member
    Posted 2 years ago #

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

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags