0

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.

1
  • In other words .= means append Commented May 3, 2017 at 11:22

2 Answers 2

0

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?

Sign up to request clarification or add additional context in comments.

4 Comments

I want one div per post.
Tried this and still produces more than one. It might be something else. I have a if statement in the loop and if it matches I include another php script that echoes the classname. Could this be it?
Can you show me where you declare $classname? The second while loop I gave you really prints only one div for each post. So something else is going on.
I have done an edit for you to see - maybe this will help>?
0

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;
}

2 Comments

I see where you are going but this wont work as I have so many fields to get from the post. I was just using classname as one example.
You can put another array into the array, of course: while ( $loop->have_posts() ) : $loop->the_post(); $ids[get_the_ID()] = array( "classname" => $post->classname, "nextvar" => "whatever"); endwhile; and then in your for loop: $variable ='<div class"'.$value["classname].;">;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.