• Resolved stoarcecreierul

    (@stoarcecreierul)


    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 https://localhost/.

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

Viewing 15 replies - 1 through 15 (of 23 total)
  • 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.

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    <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.

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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 https://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.

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

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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);
    ?>

    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?

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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 ?

    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.

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

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

    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;?>
    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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…

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

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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.

    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.

    Thread Starter stoarcecreierul

    (@stoarcecreierul)

    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.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Pages of conditioned category posts return another category’ is closed to new replies.