WordPress.org

Forums

[resolved] Pages of conditioned category posts return another category (24 posts)

  1. StoarceCreierul
    Member
    Posted 5 years ago #

    I am using in_category('#') conditional statement to change the navigation class of menu items.

    This works fine until navigating to next page, when another category is returned.

    So, I am using only 2 categories; each post belongs to only one of them. The home page displays only posts from category 2, and the conditional statement returns my desired class when browsing http://localhost/.

    The problem occurs when browsing http://localhost/page/2/, when although I see the posts from category 2, conditional statement assigns the class to category 3.

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Try dropping a copy of your conditional code into the WordPress pastebin and post the pastebin url here. Perhaps someone will be able to spot the problem and suggest a solution.

  3. StoarceCreierul
    Member
    Posted 5 years ago #

    <ul id="navigation">
    	<li<?php
                    if (in_category('2'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mh" href="<?php echo get_option('home'); ?>/">o 9 viaţă</a>
    	</li>
    	<li<?php
                    if (in_category('3'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mp" href="/proiecte/">proiecte</a>
    	</li>
                <!--these are the only 2 in_category functions used -->

    When browsing the page "proiecte" everything goes well no matter how far on the subpages I go.

    Also, when viewing single posts from both categories, it works like it should.

  4. StoarceCreierul
    Member
    Posted 5 years ago #

    Update:
    The number of posts in category 3 triggers the number of subpages that get the wrong class.

    If there are 2 posts in category 3, then http://localhost/page/2/ and /page/3/ get the wrong category. From there, /page/4/, 5 and so on get assigned to category 2 again.

    If there are 4 posts in category 3, /page/2/ ..-> /page/5/ get the wrong category. From there on things get back to normal.

    I even tried changing categories and used id's 14 and 15 - same result.
    I removed the other buttons; the menu has only the 2 items listed above.

    Currently I'm uploading the site to www so you guys could see this.

  5. esmi
    Forum Moderator
    Posted 5 years ago #

    What are you using to limit the front page display to category 2 only?

  6. StoarceCreierul
    Member
    Posted 5 years ago #

    Alright, is not the number of posts but the way they are ordered.

    I am using query_posts function to display content from only one category on the main page.

    It works like it should but it only displays the content from that category; the page still thinks that the content belongs to one of the excluded categories.

    To simplify this I will give an example with 2 categories and 1 post-per-page settings. Articles (sorted by date, like they appear in Dashboard):
    article_04 cat_id_2
    article_03 cat_id_3
    article_02 cat_id_2
    article_01 cat_id_2

    When viewing main page, article_04 is displayed, page believes there are posts belonging to category 2 on this page.
    When viewing /page/2/, article_02 is displayed but category 3 is retrieved.
    When viewing /page/3/ article_01 is displayed and category 2 is retrieved.

    I tried 2 ways of implementing query_posts; they both work on displaying the category I wish but not telling the page which category the articles displayed are in:

    <?php if ( is_home() ) {
    query_posts($query_string . '&cat=2');
    }
    ?>

    alternatively

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=2&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
    ?>
  7. esmi
    Forum Moderator
    Posted 5 years ago #

    I think you're hitting a Loop issue. Your nav menu is (presumably) either before or after the custom query. So the nav menu is effectively based on the standard query - not the custom one. My guess is that, without the custom query, you would have a post in category 3 at the top or bottom of Page 2 - which is why you're getting the "wrong" highlight on the nav menu.

    Does that make sense?

  8. StoarceCreierul
    Member
    Posted 5 years ago #

    That makes sense. The menu is in the header, the query inside index.php before the loop.

    I placed the query in the header, before the menu. When using first versionof the code, article04 appeared on all 3 pages (1 post per page) and on the second page it took the other category.

    When using the second code the results were exactly like before. Each post on its page, wrong highlight in same places again.

    So I put the query back before the loop and the nav menu after the loop. That messed up my theme, I got the menu instead of the sidebar, but it worked !

    Now we know what's causing this; how do we fix it ?

  9. esmi
    Forum Moderator
    Posted 5 years ago #

    I think you'll need to run two Loops using the same custom query - 1 for the menu and 1 for the post lists. Just remember to use wp_reset_query() between the two Loops.

  10. StoarceCreierul
    Member
    Posted 5 years ago #

    Are you saying to put the menu inside a loop? How exactly?

  11. esmi
    Forum Moderator
    Posted 5 years ago #

    What about:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $these_posts = get_posts('cat=2&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
    foreach($these_posts as $post) :
    setup_postdata($post);
    ?>
    <ul id="navigation">
    	<li<?php
                    if (in_category('2'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mh" href="<?php echo get_option('home'); ?>/">o 9 viata</a>
    	</li>
    	<li<?php
                    if (in_category('3'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mp" href="/proiecte/">proiecte</a>
    	</li>
    <?php
    break;
    endforeach;?>
  12. StoarceCreierul
    Member
    Posted 5 years ago #

    The first 2 pages are fine with their respective articles; this is a step forward as article_02 was never in the right class.

    After 2 pages the whole menu is missing. It just disspears...

  13. esmi
    Forum Moderator
    Posted 5 years ago #

    Is there an article displayed on Page 3? If so, what category is it in?

  14. StoarceCreierul
    Member
    Posted 5 years ago #

    Page 3 would be article_01 from cat_id_2; I'm keeping the example structure from the post above.

    Actually, under article_01 there are others on the same catergoy - 2; from here on, pages 4, 5... have no menu anymore.

  15. esmi
    Forum Moderator
    Posted 5 years ago #

    Can I just backtrack a little? Are you supposed to be showing post from just category 2? Only that's what:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=2&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
    ?>

    will do. No mention of category 3.

  16. StoarceCreierul
    Member
    Posted 5 years ago #

    There are posts belonging to category 2 and 3.

    On the home page only posts from category 2 will be shown, using the code above.

    On the page "/category/proiecte" only posts from category 3 will be shown.

  17. esmi
    Forum Moderator
    Posted 5 years ago #

    So this code (or template) is being used on 2 different archives?

  18. StoarceCreierul
    Member
    Posted 5 years ago #

    Only on the first page, cause by default this page shows posts from all categories.

    By using this, only category 2 is on the main page.

    The other page is a category page so by default it shows posts within that category.

  19. esmi
    Forum Moderator
    Posted 5 years ago #

    Would it not be better to have custom code for each template? One for the home page (index.php) and a second modified version for category.php?

  20. StoarceCreierul
    Member
    Posted 5 years ago #

    Ok, if it helps... What do you have in mind, what do we want to acheve with this modified category.php?

  21. esmi
    Forum Moderator
    Posted 5 years ago #

    Well, since the category page will automatically query a single category, all you need to do is add the highlighting to your menu. so:

    <ul id="navigation">
    	<li<?php
                    if (is_category('2'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mh" href="<?php echo get_option('home'); ?>/">o 9 viata</a>
    	</li>
    	<li<?php
                    if (is_category('3'))
                    { echo ' class="ales"'; }?>>
                    	<a class="mp" href="/proiecte/">proiecte</a>
    	</li>

    would work.

    If you have child cats, you could also add the post_is_in_descendant_category_function

  22. StoarceCreierul
    Member
    Posted 5 years ago #

    It does work, for the page "proiecte". It is a category page, so when browsing it or any single article from this category, the "ales" class is picked up and we are good.

    The class-choosing does not take place like it should only on the main page. The reason for this is the query_posts function inserted in index.php AFTER the conditional statement in the menu.

    The menu chooses the class according to what article he knows it would be next on that page, then the query excludes that article from showing so we end up with the menu pointing to another category.

  23. esmi
    Forum Moderator
    Posted 5 years ago #

    Can you mail a copy of your current theme to esmi at quirm dot net? I'll try to have a look at it here and see if I can figure out a solution.

  24. StoarceCreierul
    Member
    Posted 5 years ago #

    For further reference on creating custom highlighting-current-page menus, solved using the following coode before the menu:

    <?php if ( is_home() ) {
    	$cat_posts = get_posts('numberposts=1&cat=2');
    	if( $cat_posts ) {
    		foreach( $cat_posts as $post) {
    			$postid = $post->ID; // grab the first post id
    			break;
    		}
    	}
    }
    ?>

    Credits go to ESMI.

Topic Closed

This topic has been closed to new replies.

About this Topic