Tom
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: How to edit widget contents from individual postsThank you so much, I really appreciate your help! Very kind 🙂
Forum: Fixing WordPress
In reply to: How to edit widget contents from individual postsThanks @xkon
I don’t mind managing things with a plugin that creates the widgets, rather than editing the functions.php directly.
I’ve run into another issue. I’d like to register more than one Widget using my plugin – one widget for a square ad and one for a rectangle ad.
I’ve tried editing the plugin code to do so, but can’t figure it out – it either gives me a white screen or disables both widgets. If you could give me any clues on this I would be very grateful!
This is my plugin code (edited from a tutorial):
<?php /* Plugin Name: Mongoose Ad Widget Description: Allows insertion of article specific ads into sidebar */ /* Start Adding Functions Below this Line */ // Register and load the widget function wpb_load_widget() { register_widget( 'mongoose_ad_square_widget' ); } add_action( 'widgets_init', 'wpb_load_widget' ); // Creating the widget class mongoose_ad_square_widget extends WP_Widget { function __construct() { parent::__construct( // Base ID of your widget 'mongoose_ad_square_widget', // Widget name will appear in UI __('Mongoose Ad - Square', 'wpb_widget_domain'), // Widget description array( 'description' => __( '340x295 ad for Mongoose Article sidebar', 'wpb_widget_domain' ), ) ); } // Creating widget front-end public function widget( $args, $instance ) { // This is where you run the code and display the output echo $args['after_widget']; global $wp_query; $postid = $wp_query->post->ID; echo '<div style="margin-bottom:30px;">'; echo get_post_meta($postid, 'article_advert', true); echo '</div>'; wp_reset_query(); } // Widget Backend public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'wpb_widget_domain' ); } // Widget admin form ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> </p> <?php } // Updating widget replacing old instances with new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; return $instance; } } // Class wpb_widget ends here /* Stop Adding Functions Below this Line */ ?>Thank you! Any questions please ask 🙂
Forum: Fixing WordPress
In reply to: How to edit widget contents from individual postsThanks again for pointing me in the right direction – I did some googling and found a way to achieve this.
1. Created a plugin
http://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/2. Built a custom widget
http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/3. Displayed custom meta info in the widget
http://www.wpbeginner.com/wp-themes/how-to-display-custom-fields-outside-the-loop-in-wordpress/Happy days 🙂
Forum: Fixing WordPress
In reply to: How to edit widget contents from individual postsHey @xkon thanks for your reply. You’ve got it exactly!
My theme allows me to add custom meta fields to each post so I assume I just need to create my own sidebar widget that can read this meta data?
If you could give me some guidance that would be great! I have some experience editing WP code so should be able to understand… 🙂