WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A little PHP syntax help please? (9 posts)

  1. pshero
    Member
    Posted 5 years ago #

    Hey Gang,
    I'm creating lists in the sidebar by category and want to be able to check to see if a certain list-item is the "current" page. If the statement is true, then class="current"

    My existing li script looks like this:

    <li><a href="<?php echo get_permalink() ?>" class="pageid_<?php the_id(); ?>"><?php the_title(); ?></a></li>

    And although this allows me to use the pageid tag in the body id plus the page id in the link to create a custom style, it's a ton of CSS. What I'd really like is a statement that says:

    if the current page id is the same as the page id of this list-item link, then echo class="current"

    Thanks in advance for any help you can lend!

  2. dressedinvalue
    Member
    Posted 5 years ago #

    Maybe this will help. Here's the full page, and here's the code from it that I think will help:

    <ul id="menu">
    
            <!-- To show "current" on the home page -->
            <li<?php
                    if (is_home())
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>">Home</a>
            </li>
    
            <!-- To show "current" on the Archive Page (a listing of all months and categories), individual posts, but NOT individual posts in category 10 -->
            <li<?php
                    if (is_page('Archive') || is_single() && !in_category('10'))
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>/archive">Archive</a>
            </li>
    
            <!-- To show "current" on any posts in category 10, called Design -->
            <li<?php
                    if (is_category('Design') || in_category('10') && !is_single())
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>/category/design">Design</a>
            </li>
    
            <!-- To show "current" on the About Page -->
            <li<?php
                    if (is_page('About'))
                    {
                    echo " id=\"current\"";
                    }?>>
                    <a href="<?php bloginfo('url') ?>/about">About</a>
            </li>
    </ul>
  3. SimonJ
    Member
    Posted 5 years ago #

  4. SimonJ
    Member
    Posted 5 years ago #

    Sorry... We posted in the same time :-)

    S.

  5. dressedinvalue
    Member
    Posted 5 years ago #

    Great minds. :)

  6. pshero
    Member
    Posted 5 years ago #

    Thanks guys, my issue though is that that links are being generated dynamically (I'm using WP as a CMS). So I'm hoping to use the commonality between the dynamic list item and either the post ID or the slug to return the "common" class. Does that make sense? By doing this any new content added in the category would automatically have the correct class when it was the current page. Otherwise every time my client adds a new page, I'll have to go in and hard code more if statements.

  7. pshero
    Member
    Posted 5 years ago #

    So basically I just need to know the correct php syntax for writing this (the if statement):

    <li><a <?php if(is_single('post_id()')); { ?>class="current"<?php } ?> href="<?php echo get_permalink() ?>"<?php the_title(); ?></a></li>
  8. pshero
    Member
    Posted 5 years ago #

    Or simply, how would the php for this phrase be written?

    if (is_single('call the post id of this list-item'));

    If I can figure out that phrase, I can get this to work.

  9. pshero
    Member
    Posted 5 years ago #

    Another hour of searching and I found a snippet that helped me solve the issue. It's here if anyone's looking for something similar.

    http://wordpress.org/support/topic/212323?replies=4

Topic Closed

This topic has been closed to new replies.

About this Topic