Forums

Work with index.php to display a tab for created categories (33 posts)

  1. jhames
    Member
    Posted 3 years ago #

    I should preface this question with a statement: I don't have "mad crazy skillz" with PHP so feel free to speak slowly and use exaggerated hand movements.

    I'm using WP for a client's site to manage their news, events and press releases. Ideally, they'd like the homepage to display 3 tabs, 1 tab for each category. News is open by default, the other two tabs are inactive. You click and tab and presto! the posts related to that category appear.

    Is it as simple as assigning a tab to a category URL? In other words, <a href="#" rel="/wordpress/?cat=3">News</a>

  2. buddha trance
    Member
    Posted 3 years ago #

    In header.php of the theme you are using, change <?php wp_list_pages; ?> with <?php wp_list_categories; ?>.

    That should do it.

  3. buddha trance
    Member
    Posted 3 years ago #

    One more thing. If they are creating more than 3 categories, but they only want 3 tabs to show, then use template tags for wp_list_categories.

    If they will only create 3 total, than it's fine as above.

  4. jhames
    Member
    Posted 3 years ago #

    Thank you for the tip on wp_list_categories, buddha trance. I'm using the following code to define the category tabs:

    <?php wp_list_categories('orderby=name&include=3,4,5' . 'exclude&title_li='); ?>

    The next step, after I apply CSS to each list item and create tabs, is to link each tab with its category. Is there a simple way to wrap an a tag around the categories?

  5. jhames
    Member
    Posted 3 years ago #

  6. buddha trance
    Member
    Posted 3 years ago #

    The links should be automatically created, once the categories are listed with wp_list_categories.

    Assign a category to each relevant post, and when you click on the tab, it will take you to the list of posts of that category.

    It seems like you already have that working for you.

    So, all you have to do is to create new categories in admin --> Manage --> Categories --> Add New, and then assign them to each post.

    I am understanding the question correctly?

  7. jhames
    Member
    Posted 3 years ago #

    Totally follow you there. What I want to do is lead with "News" which has a white tab to indicate it's selected. I thought I could somehow add an argument to <?php wp_list_categories('orderby=name&include=3,4,5' . 'exclude&title_li='); ?> but I'm not sure where to place current_category=1 in the tag. I tried include=3,4,5&current_category=1 but that didn't seem to work.

  8. buddha trance
    Member
    Posted 3 years ago #

    I see, you want to have active tabs.

    Do it in the style.css of your theme, with a ul class="menu" in your theme header.php

    ul.menu li a {
    color:#fff;
    }

    Let me know if this worked for you.

  9. jhames
    Member
    Posted 3 years ago #

    Part One:

    Each tab should have an off and on state. News, by default, will be on and therefore white. The other two tabs will be gray.

    If wp_list_categories can set the News tab to current, then I can use CSS to give it a white tab background.

    Part Two:

    Within <?php if (have_posts()) : ?>, do I need to add, delete or edit any of the default code to tell WP that I only want to see entries under category "News" because the tab "Latest News" is active?

  10. buddha trance
    Member
    Posted 3 years ago #

    Part One:

    Sorry, I gave you the wrong part of the code...

    See, the funny part is that I do have active tabs, in my template. Looking there for reference.

    style.css

    ul.menu li.current_cat a, ul.menu li.current_cat a:hover {
    background:#fff;
    }

    In header.php, try this way

    <li class="current_cat=1<?php } else { ?>cat_item<?php } ?>"><a title="<?php bloginfo('name'); ?>" href="<?php bloginfo('url'); ?>">News</a></li>
    
    	<?php wp_list_categories('depth=1&title_li=&exclude='); ?>

    Part Two:

    I don't think so, because the categories already pull the posts tagged with them.

  11. buddha trance
    Member
    Posted 3 years ago #

    I found this reference about category lists.

  12. jhames
    Member
    Posted 3 years ago #

    I tried your code and the following list appeared:

    • News
    • Events
    • Latest News
    • Press Releases

    I removed the first list item tag and my result was the same as before but without "News".

    *sigh*

  13. buddha trance
    Member
    Posted 3 years ago #

    Let me download your theme...

    Looking at your page now, the tabs are gone. Just a list.
    Hold on...

  14. buddha trance
    Member
    Posted 3 years ago #

    I looks like you customized the style from the Default theme, is that right?

  15. buddha trance
    Member
    Posted 3 years ago #

    jhames,

    correction to Part Two, as I noticed the home page still pulls the main loop.

    Yes, edit using query posts

    <?php
    query_posts('cat=3');      //retrieves category 3 only
    ?>
  16. jhames
    Member
    Posted 3 years ago #

    I did customize the style from the Default theme. Is it possible to ignore header.php, sidebar.php and footer.php, and simply work with index.php?

  17. buddha trance
    Member
    Posted 3 years ago #

    jhames, in this thread there is the correct code to display a category on your home page, in case you are still trying to figure it out!

  18. jhames
    Member
    Posted 3 years ago #

    Holy Dhammapada, buddha trance, it worked! Now I can view only the News entries on the main page.

    ...Now I just need WP to display the "News" tab with the class "current_category".

  19. buddha trance
    Member
    Posted 3 years ago #

    jhames,

    glad it worked!

    Regarding the tabs, I have been working on something that may be just like your case and was able to find a solution for this last night, by keeping the entire original "current_page" code, creating a page called like the category, and then using a template.

    Let me know if you want to try it the way I did, and I will tell you the steps.

  20. jhames
    Member
    Posted 3 years ago #

    I found a link via Google on a WordPress Navigation Bar and I am trying to adjust its use on the page.

    <?php //highlight 'Blog' if not Page
    if (is_page()) {
    $highlight = "page_item";
    } else {
    $highlight = "page_item current_page_item";
    }
    ?>
    <div>
    <ul class="tabs">
    <li class="<?php echo $highlight; ?>"><a href="<?php echo get_settings('home'); ?>">Latest News</a></li>
    <?php wp_list_categories('title_li='); ?>
    </ul>
    </div>

    Can your solution be done within the default pages provided by WP? I'm only asking because I have limited server access.

  21. jhames
    Member
    Posted 3 years ago #

    I found a link via Google on a WordPress Navigation Bar and I am trying to adjust its use on the page.

    <?php
    if (is_page()) {
    $highlight = "page_item";
    } else {
    $highlight = "page_item current_page_item";
    }
    ?>
    <div>
    <ul class="tabs">
    <li class="<?php echo $highlight; ?>"><a href="<?php echo get_settings('home'); ?>">Latest News</a></li>
    <?php wp_list_categories('title_li='); ?>
    </ul>
    </div>

    Can your solution be done within the default pages provided by WP? I'm only asking because I have limited server access.

  22. jhames
    Member
    Posted 3 years ago #

    I found a link via Google on a WordPress Navigation Bar and I am trying to adjust its use on the page.

    <?php
    if (is_page()) {
    $highlight = "page_item";
    } else {
    $highlight = "page_item current_page_item";
    }
    ?>
    <div>
    <ul class="tabs">
    <li class="<?php echo $highlight; ?>"><a href="<?php echo get_settings('home'); ?>">Latest News</a></li>
    <?php wp_list_categories('title_li='); ?>
    </ul>
    </div>

    Can your solution be done within the default pages provided by WP? I'm only asking because I have limited server access.

  23. jhames
    Member
    Posted 3 years ago #

    Sorry for the multiple replies, WP didn't show my first reply as posted.

  24. buddha trance
    Member
    Posted 3 years ago #

    Try that code first to see if it works for you, it may be simpler.

    My solution involves creating template pages. That means uploading 3 files to the server, for your 3 category tabs, into your theme folder. It fairly easy, though.

    Then you have to adjust style.css to highlight the current tab. I am using that on my site, so I can give you my code, and you can adjust the styling to fit yours.

  25. jhames
    Member
    Posted 3 years ago #

    Mother Mary Magdalene, I got it to work!

    I now have the active tab using the "on" state with the other two tabs using the "off" state, AND only posts associated with "news" are displaying on the homepage.

    Now one last trick: when the user clicks on "events"

    • how to hide the posts related to "news"
    • how to highlight "events" with the "on" state tab

    WP is really cool, I only wish so much of my questions weren't dependent on PHP. :\ But thank you so much for all of your assistance, buddha trance! I couldn't have made it this far without you. :)

  26. jhames
    Member
    Posted 3 years ago #

    Reading your last response a second time, I think your solution is far, far simpler.

    I better get on that server access asap.

  27. buddha trance
    Member
    Posted 3 years ago #

    jhames,

    I just looked at your site again. Well, congratulations for the first tab!
    Yes, the templates may be the way to go, because you are showing "Archives" for the other tabs.

    That way, you will have 3 pages, each showing only that category. With 3 active tabs, each on and off.

    Let me tell you what you need to do with my method, so that others may benefit from this thread, in case they need a similar thing. Will post in a few minutes, so I can look at my files again.

  28. buddha trance
    Member
    Posted 3 years ago #

    1 - CREATE THE TEMPLATES

    Start your new template page (which you call events.php) with

    <?php /*
    	Template Name: Events
    */ ?>

    then, right underneath, copy and paste the entire content of index.php of your theme.
    Do the edit below:

    Right in between <?php if(have_posts()) : ?> and <?php while(have_posts()) : the_post(); ?> you add the query of the category you want to display (change the number to your own category ID, of course), like this:

    <?php if(have_posts()) : ?>
    		<?php
       if (is_page()) {
          query_posts("cat=29");
       }
    ?>
    <?php while(have_posts()) : the_post(); ?>

    This is the same thing you should have in your "Latest News", because it's the index page with the query for that category. Right?

    Repeat the steps and do the same for the other "Press Releases" template (press.php), with the appropriate category ID in the query.

    Upload the files into your theme folder, just where you have sidebar.php, header. php, index.php, etc.

    2 - CREATE THE PAGES IN WORDPRESS

    Go to your Admin --> Write --> Page, and create two new pages (this is the name that will appear in the tabs), and call one "Events" and the other one "Press Releases".

    Don't put any text in there, leave blank, BUT from the "Page template" drop down menu, pick the right template you have created for each. Save and publish.

    3 - THE MENU IN THE HEADER

    In header.php of your theme, use the call to the pages for your menu (I have changed "Home" with "Latest News" for you, which is the call to the index.php page you already have with the news category query)

    <ul class="menu">
    <li class="<?php if ( is_home() or is_archive() or is_single() or is_paged() or is_search() or (function_exists('is_tag') and is_tag()) )
    	{ ?>current_page_item<?php } else { ?>page_item<?php } ?>"><a title="<?php bloginfo('name'); ?>" href="<?php bloginfo('url'); ?>">Latest News</a></li>
    
    	<?php wp_list_pages('depth=1&title_li=&exclude='); ?>
    </ul>

    4 - THE STYLE.CSS

    This is the call to the classes mentioned in the header. Adjust according to the look, colors, etc. you want.

    /*-----main-menu*/
    	ul.menu{position:absolute; right:25px; padding:5px 10px 0;}
    	ul.menu li{float:left; margin:0 0 0 5px; background:#777; font:bold 11px/1 'trebuchet ms', arial, sans-serif;}
    	ul.menu li a{display:block; color:#fff; padding:5px 10px;}
    	ul.menu li a:hover{text-decoration:none; background:#888;}
    	ul.menu li.current_page_item a, ul.menu li.current_page_item a:hover{color:#444; background:#ccc;}

    ET VOILA'!!!! I don't think I forgot any steps... Let me know if this worked for you!

  29. jhames
    Member
    Posted 3 years ago #

    I just got server access this afternoon. I'll try your solution and let you know asap the results.

  30. jhames
    Member
    Posted 3 years ago #

    Oh dear. I had unexpected results.

    WordPress isn't including <div> tags I wrote, but at the same time it's inserting code I don't want. I'm sorting through my events.php page right now trying to uncover why <div> tags didn't get published.

Topic Closed

This topic has been closed to new replies.

About this Topic