WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom fields not being called in custom widget (3 posts)

  1. Maxaud
    Member
    Posted 4 years ago #

    I'm using the following code to call some featured posts and widgets in a side scrolling belt on my site:

    <div id="myslides">
    	<?php
    		$my_query = new WP_Query('cat=4&showposts=24');
    	?>
    	<div class="leftone"><a href="javascript:stepcarousel.stepBy('mygallery', -1)"><img src="<?php bloginfo('template_url'); ?>/images/left.jpg" alt="Left" /></a></div>
    	<div class="rightone"><a href="javascript:stepcarousel.stepBy('mygallery', +1)" ><img src="<?php bloginfo('template_url'); ?>/images/right.jpg" alt="Right" /></a></div>
    	<div id="mygallery" class="stepcarousel">
    		<div class="belt">
    			<div class="panel">
    				<?php
    					while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID;
    				?>
    					<?php post_style(); ?>
    					<?php
    						$featuredimage = get_post_meta($post->ID, 'featuredimage', $single = true);
    					?>
    					<div class="panelimage">
    						<?php if ($featuredimage) { ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img class="featuredimage" src="<?php echo $featuredimage; ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" /></a><?php } else {  echo('');  } ?>
    					</div>
    
    				<?php endwhile; ?>
    			</div>
    		</div>
    	</div>
    </div>

    When I implement the above code directly into my sidebar it works fine, but I'm trying to put it into a widget so it can be easily moved.

    I add it to a widget using the following code:

    <?php
    /**
     * Plugin Name: Featured On Scroller
     * Plugin URI: http://playforward.net
     * Description: A widget that adds the "Featured On" scroller to the sidebar.
     * Version: 1.0
     * Author: Dustin Dempsey
     * Author URI: http://playforward.net
     */
    
    add_action( 'widgets_init', 'scroller_load_widgets' );
    
    function scroller_load_widgets() {
    	register_widget( 'scroller_Widget' );
    }
    
    class scroller_Widget extends WP_Widget {
    
    	function scroller_Widget() {
    		/* Widget settings. */
    		$widget_ops = array( 'classname' => 'scroller', 'description' => __('A widget that adds the Featured On scroller to the sidebar.', 'scroller') );
    
    		/* Widget control settings. */
    		$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'scroller-widget' );
    
    		/* Create the widget. */
    		$this->WP_Widget( 'scroller-widget', __('Featured On Scroller', 'scroller'), $widget_ops, $control_ops );
    	}
    
    	function widget( $args, $instance ) {
    		extract( $args );
    		 ?>
    
    <script type="text/javascript">
    stepcarousel.setup({
    	galleryid: 'mygallery', //id of carousel DIV
    	beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
    	panelclass: 'panel', //class of panel DIVs each holding content
    	autostep: {enable:false, moveby:1, pause:12000},
    	panelbehavior: {speed:500, wraparound:true, persist:true},
    	defaultbuttons: {enable: false, moveby: 2},
    	statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
    	contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']
    })
    </script>
    <div id="myslides">
    	<?php
    		$my_query = new WP_Query('cat=4&showposts=24');
    	?>
    	<div class="leftone"><a href="javascript:stepcarousel.stepBy('mygallery', -1)"><img src="<?php bloginfo('template_url'); ?>/images/left.jpg" alt="Left" /></a></div>
    	<div class="rightone"><a href="javascript:stepcarousel.stepBy('mygallery', +1)" ><img src="<?php bloginfo('template_url'); ?>/images/right.jpg" alt="Right" /></a></div>
    	<div id="mygallery" class="stepcarousel">
    		<div class="belt">
    			<div class="panel">
    				<?php
    					while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID;
    				?>
    					<?php post_style(); ?>
    					<?php
    						$featuredimage = get_post_meta($post->ID, 'featuredimage', $single = true);
    					?>
    					<div class="panelimage">
    						<?php if ($featuredimage) { ?><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img class="featuredimage" src="<?php echo $featuredimage; ?>" title="<?php the_title(); ?>" alt="<?php the_title(); ?>" /></a><?php } else {  echo('');  } ?>
    					</div>
    
    				<?php endwhile; ?>
    			</div>
    		</div>
    	</div>
    </div>
    
    	<?php
    	}
    
    	function update( $new_instance, $old_instance ) {
    		$instance = $old_instance;
    
    		return $instance;
    	}
    
    	function form( $instance ) {
    
    		<p>There are no settings for this widget</p>
    
    	<?php
    	}
    }
    
    ?>

    The above code will query the posts but it wont pull in the custom fields for those posts.
    The widget is added into my functions.php theme file.

    Thanks in advance for any assitance.

  2. Maxaud
    Member
    Posted 4 years ago #

    Also, when my sidebar looks like this:

    <div id="sidebar">
    	<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Homepage Sidebar') ) : else : ?>
    
    		<h3 class="">Featured on:</h3>
    		<?php include (TEMPLATEPATH."/slide.php"); ?>
    
    	<?php endif; ?>
    </div>

    But when I simply have the widget output:
    <?php include (TEMPLATEPATH."/slide.php"); ?>

    it still doesn't work..

  3. Maxaud
    Member
    Posted 4 years ago #

    It works once I put
    <?php global $post; ?>
    within my loop before the custom field code.

Topic Closed

This topic has been closed to new replies.

About this Topic