I’m trying to make a very specific widget, but I can’t get past this one major issue. The widget creates a loop to display information from a specific custom post type. The important thing is that it’s supposed to allow the developer to create a custom layout, but there’s also a default if I don’t enter anything. I have been able to get most everything working, including allowing PHP to execute in the plugin. But the problem is that when I create the custom layout in the widget, it starts pulling information from the basic posts rather than my custom post type. However, if I put that code directly into the PHP file rather than in the widget, it executes as expected. Here is the front end code:

<?php

echo $before_widget;
echo $before_title . $title . $after_title;

//The Loop
$args = array( 'post_type' => 'ctas', 'posts_per_page' => $num_ctas );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="<?php echo $cta_class; ?>">
    <?php
      if(!empty($cta_layout)){
        echo $cta_layout; //this is the code entered into the widget that doesn't execute as expected
      } else { ?>
    <h3><?php the_title(); ?></h3> <!--this part works fine-->
    <?php the_content(); ?>
    <?php } ?>
  </div>
<?php endwhile;

//After The Widget
echo $after_widget;
?>

Read more here: PHP returned from widget doesn’t take on loop parameters


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: