Reputation: 80
I have a loop that gets all the posts that are "custom_post". It works perfectly - apart from that when I echo a variable it duplicates the variable when there are more than one post.
Its hard to explain - but basically if I have one post it works perfectly. I get a Div with the class name that is assigned to that custom post.
When I add another post - again that works, but displays two divs with the second post. I would imagine that it has something to do with echo $variable in the loop.
Any ideas? Thanks
EDIT
CODE:
function display_css() {
$ids = array();
$args = array( 'post_type' => 'custom_post');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
array_push( $ids, get_the_ID() );
endwhile;
foreach (array_unique($ids) as $key => $value) {
$check_select_modules = $titan->getOption( 'selec_modules', $value );
if ( "accordianmodule" == $check_select_modules) {
include(DE_DB_PATH . '/lib/modules/accordian.php');
}
elseif ( "textmodule" == $check_select_modules) {
include(DE_DB_PATH . '/lib/modules/text.php');
}
else {
}
}
}
add_action( 'wp_head', 'display_css', 15 );
Then in one of the php scripts it has
$css_accordian .= '<style id="css-'.$accordian_module_heading_css_class_display.'">';
echo $css_accordian;
I get the variable $accordian_module...... further up the script.
Upvotes: 0
Views: 768
Reputation: 369
The problem is that $classname is not being set in that piece of code you have copied here. I'm guessing it gets assigned some value before this code? I recommend set the classname into the $ids array (though you should call that something else then) during the while loop. something like this, though "$post->classname" probably won't work, you'll have to figure out where to get classname from.
while ( $loop->have_posts() ) : $loop->the_post();
$ids[get_the_ID()] = $post->classname;
endwhile;
Then in the for loop, the $value will contain your classname, so you can do this:
foreach (array_unique($ids) as $key => $value) {
$variable ='<div class"'.$value.;">;
echo $variable;
}
Upvotes: 0
Reputation: 553
First of all you miss a quote here:
$variable .= '<div class"'.$classname.;">;
should be:
$variable .= '<div class"'.$classname.'">';
Suggestion:
$ids = array();
$args = array( 'post_type' => 'custom_post');
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
array_push($ids, get_the_ID());
}
}
However.. instead of using a foreach to loop through all the ID's for a second time, why you not echo a div in side the loop above, like this:
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
echo '<div class="'.$classname.'">' . the_ID() . '</div>';
}
}
Or do you want one div where all the posts are shown?
Upvotes: 0