0
votes

Thank you in advance for any assistance. Building a wordpress template and i have a section for Locating Representatives based on state. So i have set up a custom post type called Reps and am using custom fields to populate the contact info of each representative, along with a checkbox selector for which states that person is assigned to (some representatives are assigned to multiple states).

On the Rep Locator page I have a div with a list of links, one for each state, and then a content display div that will hopefully list any Reps posts that have that state assigned to them with the ACF checkbox selector. What's the simplest way to populate that content div with the Reps posts' content based on which state link the user clicks. Don't want to make a new page for each state in the USA to manually query the posts' content.

I thought about trying to pass PHP variables in the urls of the links, like /rep-locator.php?state=california ... is there any way to do this with links in a wordpress template? Or would something like AJAX be a better fit (probably over my head at this point)?

Hopefully this makes sense, and thank you very much for any consideration!

2

2 Answers

0
votes

If you're looking for a quick way to do it, there's a WP plugin you may want to try Blog-in-Blog plugin wordpress.org/plugins/blog-in-blog

You may also want to check out WordPress 3.0 Menus Subpanel (just go to Dashboard > 'Appearance' > 'Menus').

If you want to do this without a plugin, you may: - Create a taxonomy using the CPT UI plugin - Assign the taxonomy to your custom post type - Add the list of states to your taxonomy (as tags) - Create a single category template named "category.php" and add it to your template. - Inside the template, alter the template query by using the term object name (or tag):

<?php 

//get the term object
$term_id = get_queried_object_id(); 
$term_object = get_term_by('id', $term_id, 'blog');

//alter the query
$args = array(
	'posts_per_page' => '1',
	'tax_query' => array(
		array(
			'taxonomy' => 'states',
			'field' => 'slug',
			'terms' => $term_object->name,
		),
	),
);

//create the query
$query = new WP_Query( $args );

?>
0
votes

Why not using custom taxonomies instead of custom fields for the state ? Then you can call directly the archive of the given taxonomy to display the list of representatives in each state. Moreover, requests will be a lot easier and faster because taxonomies are especially done for your kind of problem.

Eventually, if you don't want to type the state in the taxonomy field each time you create a representative, you could use a custom field to chose the state in a list by using a Taxonomy type custom field and then choose how to display it. Sometimes useful, sometimes not (especially with looooong lists). I advice you to place this custom field in the sidebar then...