WordPress.org

Ready to get started?Download WordPress

Forums

Query post categories based on current page (5 posts)

  1. atlanteavila
    Member
    Posted 2 years ago #

    Hi all, I have been trying to figure out how to create a switch function to display certain queries in certain pages.

    What I'm trying to achieve is this:

    I have a client who has three studios, in each studio the client has different contact lists. So I've created 3 different post types (sac_contacts, sf_contacts and sj_contacts) they all hold different information, the problem is that I currently have to create three different pages and three different page templates, this problem occurs a lot because I have like 17 different sections where this is the case for example each studio uses their own downloads section for employees, each studio has downloads for contracts based, and the list goes on and on, so I wanted to save me some time and figure out how to create some sort of switch loop that changes the category to display based on the page title it is on.

    So for instance if someone visits Sacramento Studio Downloads I want to create one loop that recognizes, hey I'm on the Sacramento Downloads page so I will only display posts from the custom posts type sac_downloads and on and on...

    I've got this but doesn't work at all:

    <?php if (is_page('Sacramento Contacts')) {
    			$queryA="$my_query = new WP_Query('post_type=sac_contacts&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();";
    		} elseif (is_page('San Francisco Contacts')) {
    			$queryA="$my_query = new WP_Query('post_type=sf_contacts&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();";
    		} elseif (is_page('San Jose Contacts')) {
    			$queryA="$my_query = new WP_Query('post_type=sj_contacts&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();";
    		} else {
    		return;
    		}
    	}
    ?>  
    
      <?php echo $queryA ?>

    PLEASE HELP THIS IRRITATED SOUL!!!

  2. richarduk
    Member
    Posted 2 years ago #

    I would have gone down this road

    <?php if (is_page('Sacramento Contacts')) {?>
    
    			$my_query = new WP_Query('post_type=sac_contacts&showposts=-1'); while ($my_query->have_posts()) : $my_query->the_post();
    
    		<?php } elseif (is_page('San Francisco Contacts')) {

    but before doing all the if elses I would have just stuck one loop in there and seen if that worked, and if not dug down into that loop.

    I don't know what your errors etc. are so it's difficult to say any more.

  3. richarduk
    Member
    Posted 2 years ago #

    Sorry, didn't add php tags round the line of code ...

  4. atlanteavila
    Member
    Posted 2 years ago #

    Thanks Richarduk! Can you specify what you meant by ..."stuck one loop in there..." Stuck it in where?

  5. richarduk
    Member
    Posted 2 years ago #

    Yeah, sorry, in essence I'm trying to say, if it doesn't work, simplify everything. So by 'stick one loop in there' I meant replace that block of if/else with just one loop to see if that alone worked.

    If one loop did work then the problem is in your pasted block of code as a whole.

    If one loop didn't work then the problem is maybe in the line of code that creates the loop, in which case try a different way of creating the loop.

    That sort of thing. Just simplify in order to figure it out.

Topic Closed

This topic has been closed to new replies.

About this Topic