• So i have a list of the all posts in a particular category. I would like to display a class on the current page. I have the below code working…but it only works outside of the loop which doesn’t help me when I am displaying a list of post titles. Does anyone have any ideas how to do this within a loop?

    <?php $current=$post->ID; ?>

    <?php if ( is_single($current) ) {
    echo ‘id=”current”‘;
    } else {}
    ?>

Viewing 11 replies - 1 through 11 (of 11 total)
  • Does your Theme have the body_class() template tag inside the HTML <body> tag? e.g.

    <body <?php body_class(); ?>>

    Thread Starter kaylee

    (@kaylee)

    yes

    Then you should already be displaying the appropriate CSS class for the category.

    Can you view your browser source, and paste what is listed in:

    <body class="...">

    Thread Starter kaylee

    (@kaylee)

    <body class=”page page-id-2 page-parent page-template page-template-page-about-php logged-in admin-bar”>

    Note: I don’t want to have to write a bunch of if is_single(’30’) conditional tags. I would like it to be dynamic. If I can display the post id outside the loop, can i then apply it to a variable and match it to that post in the loop?

    <blockqoute><body class="page page-id-2 page-parent page-template page-template-page-about-php logged-in admin-bar">

    You are currently viewing the “About” Page, which is a static Page, not a Post.

    Pages do not use the category taxonomy.

    Can you post the body class output from the Post you referenced in your original post?

    Thread Starter kaylee

    (@kaylee)

    Well I’ve got a static page that outputs a list of post titles on the sidebar.

    This would be the body tag…
    <body class=”page page-id-4 page-template page-template-page-services-php logged-in admin-bar”>

    Well I’ve got a static page that outputs a list of post titles on the sidebar.
    This would be the body tag…
    <body class=”page page-id-4 page-template page-template-page-services-php logged-in admin-bar”>

    Okay, now we’re getting somewhere… you’ve got a Static Page, with some content, and in the sidebar, you have a list of Post Titles.

    So, can you explain in more detail what you’re trying to do?

    And can you post the code that is used to output this list of Post Titles?

    Thread Starter kaylee

    (@kaylee)

    Well I am running a loop to display the titles and I would like to add a class to the current post that is being viewed so the user knows where they are.

    here’s what i have so far…

    <?php //get the id of the page, only works outside the loop ?>
    
    <?php $current=$post->ID; if ( is_single($current) ) {
    	//echo 'id="current"';
    	} else {} ?>
    
    <?php // to run the list ?>
    <?php
    query_posts('cat=9&order=DESC'); if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <li><a>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

    [please make sure to mark your code either using backticks or the ‘code’ button]

    Well I am running a loop to display the titles and I would like to add a class to the current post that is being viewed so the user knows where they are.
    here’s what i have so far…
    <?php //get the id of the page, only works outside the loop ?>
    <?php $current=$post->ID; if ( is_single($current) ) {
    //echo ‘id=”current”‘;
    } else {} ?>
    <?php // to run the list ?>
    <?php
    query_posts(‘cat=9&order=DESC’); if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    “><?php the_title(); ?>
    <?php endwhile; ?>

    Have you tried adding the post_class() template tag somewhere in your custom Loop?

    Thread Starter kaylee

    (@kaylee)

    Oh! I will try that…

    Thread Starter kaylee

    (@kaylee)

    Got it! You have run the get post id outside the loop, put that into a variable, and then run it a second time and compare the two.

    the code…

    // run this outside the loop
    <?php $postid = get_the_ID(); ?>
    
    //inside the loop
    <?php $current = get_the_ID();  ?>
    
    //where you want the class to be
    <?php if ($current == $postid) { echo 'class="current"'; }
    	else {  }
    	?>

    [Please post code snippets between backticks or use the code button.]

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘display class on current page’ is closed to new replies.