Jump to content

Help with php widget link


AV88

Recommended Posts

Hi all,

 

I've got a problem with a php code on my website. I don't know why, but the hyperlink is wrong, because it's taken also the link of the current page, before the one of the page of destination. (see the attachment)

 

Untitled-2.jpg

 

Where is the mistake?

Thanks all for the help :)
 

			$post_id = $instance['posts_name'];
			
			$title = apply_filters('widget_title', $instance['title']);		
			if ( empty($title) ) 
					$title = false;
		
			echo '<div class="widget top-story">';
			
			
			if ( $title )
			echo $before_title . $title . $after_title . "\n"; ?> 
				<div>
        		<?php $post = get_post( $post_id ); ?>
                                
                                <a href="<?php echo $post->post_name; ?>" class="image"><?php echo get_the_post_thumbnail( $post_id, 'top-story'); ?><span class="comment-icon"><?php echo $post->comment_count; ?></span></a>
                            	<span><?php the_time('d.m.Y  |  g:i A'); ?></span>
                             	<span class="rating"><a href="#" class="active"></a><a href="#" class="active"></a><a href="#"></a><a href="#"></a><a href="#"></a></span>
                                <p><a href="<?php echo $post->post_name; ?>"><?php echo $post->post_title; ?></a></p>                                

				</div>
			
		<?php echo '</div>';
		}
		
		/* Update widget settings */
		function update( $new_instance, $old_instance ) {
			$instance = $old_instance;
			$instance['title'] = strip_tags( $new_instance['title'] );
			$instance['posts_name'] = $new_instance['posts_name'];
			
			return $instance;
		}
		
		/* Widget form */
		function form( $instance ) {
			
			$defaults = array( 
				'title' => '',
				'posts_name' => '',
			);
			
			$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
				<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
			</p>
            <p>
			<label for="<?php echo $this->get_field_id('posts_name'); ?>">Choose Post:</label>
			<select name="<?php echo $this->get_field_name('posts_name'); ?>" id="<?php echo $this->get_field_id('posts_name'); ?>" class="widefat">
                <?php $posts_array = get_posts( array('numberposts'     => -1) ); ?>
				<?php foreach( $posts_array as $post ) : setup_postdata($post); ?>
						<option value="<?php echo $post->ID; ?>"<?php selected( $instance['posts_name'], $post->ID ); ?>><?php echo $post->post_title; ?></option>
				<?php endforeach; ?>
			</select>
			</p> 
			
			<?php 
		}
	}
Link to comment
https://forums.phpfreaks.com/topic/291907-help-with-php-widget-link/
Share on other sites

This is the reason why you should always put a fully qualified URI in an anchor tag. Putting relative links into an anchor tag, if not well thought out, will create the problem you are having.

In other words:

BAD

 

<a href="/my/links">Links</a>

Good

 

<a href="http://my.site.com/my/links">Links</a>

 

You will have less of these problems.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.