I am trying to make a toggle function work, but since I have only basic PHP knowledge, I just don’t know how to get my <div>‘s straight… Can somebody please help me?
My problem is not the (jQuery) toggle, but simply a combination of HTML/PHP used to get WordPress Custom Post Type specific content from the database. That worked fine with the code I have, but now I simply don’t know how to wrap the output into divs needed for my jQuery function.
So first:
Print out all meta_values by post_type. In this case the post_type = artists and the meta_value = artist_country.
I printed all artists belonging to a specific country like so:
Argentina (artists_country)
- Capitan Tifus (artist title)
- Kapanga (artist title)
Here comes my problem with the divs. I need to group (using ‘main1’) the artist titles that belong to a country, but without grouping the country itself.
<div class="group">
<h3><a class="trigger">Country 1</a></h3>
<div id='main' class='side-toggle'>
<h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 1 of that country</a></h4>
<h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 2 of that country</a></h4>
</div><!--main--></div><!--group-->
<div class="group">
<h3><a class="trigger">Country 2</a></h3>
<div id='main1' class='side-toggle'>
<h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 1 of country 2</a></h4>
<h4 class="date"><a href="<?php the_permalink(); ?> rel="fancybox" title="the title">The Artist 2 of country 2</a></h4>
</div><!--main--></div><!--group-->
I am trying to figure out how to group the artists. But where ever I put my divs, I get 1 artist per country, our everything get’s looped multiple times. So I am guessing it’s a problem related to how I get the content from the database?
This is my code:
// List posts by a Custom Field's values
$meta_key = 'artists_country'; // The meta_key of the Custom Field
$sql = "
SELECT p.*,m.meta_value
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
WHERE p.post_type = 'artists'
AND p.post_status = 'publish'
AND m.meta_key = '$meta_key'
ORDER BY
m.meta_value ASC,
p.post_title ASC
";
$rows = $wpdb->get_results($sql);
if ($rows) {
foreach ($rows as $post) {
setup_postdata($post);
if ($post->meta_value != $current_value) {
$current_value = $post->meta_value;
echo ('<div class="group">');
echo "<h3><a class='trigger' title='Click to expand' rel='nofollow' href='#'>$post->meta_value +</a></h2>";
echo ("<div id='main1' class='side-toggle'>");
}
// Put code here to display the post
echo ('<h4 class="date">');
echo ('<a href="');
the_permalink();
echo ('" class="postlink" rel="fancybox" title="the title">');
the_title();
echo ('</a>');
echo('</h4>');
}
echo('</div><!--main--> ');echo('</div><!--group--> ');
}
Etc…
I have a link here: http://www.musicamestiza.nl/?page_id=4311
After puzzling long time with the div’s and also trying many different ways to print posts by custom field in the above described order, moving the ending div to the start of the output did the trick some how combined with adding other divs (who don’t have any other function). (So I am starting the output with instead of .
Not a great way to go, but it served my purpose and it might someone else’s. If anybody has a better solution I of course would like to know.