• I am using the WordPress 2014 template with a child theme.
    I want to add some blocks at the bottom of my site with the image and title of other pages to link to.

    I have created a dynamic sidebar to add widgets and need the code to add to my text widget to display the Thumbnail and Title of the page which has an ID of 80.
    I got the below code from Codex but it is showing blank, any ideas?!

    <?php
    $pageID = 80;
    $page = get_post($pageID);
    echo $page->post_title;
    ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there,

    What I would do is create a shortcode at the bottom of the functions.php file, using a custom query to pull in the title and featured image of a specific post.

    First you would have to enable shortcodes in the widget area, also in your functions.php file:

    // Enables the use of shortcodes in sidebars -
    add_filter('widget_text', 'do_shortcode');

    Then you would create the shortcode:

    // shortcode: [page_sidebar] Usage example = [page_sidebar id="2" ]
    function diy_page_in_sidebar($atts, $content=null){
    
        extract(shortcode_atts( array('id' => ''), $atts));
    
         $output = '';
         $args = array(
    		'page_id' => $id,
    		);
         $custom_query = new WP_Query( $args );
    
    		if ($custom_query->have_posts()) :
    		$output .= '<div class="page_sidebar">';
    		while ($custom_query->have_posts()) : $custom_query->the_post();?>
    		<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    		<?php the_post_thumbnail('thumb');?>
    
    		<?php endwhile;
    		$output .= '</div>';
    		endif;
    		wp_reset_postdata(); 
    
    	return $output;
    
    }
    add_shortcode('page_sidebar', 'diy_page_in_sidebar');

    So the bottom of your functions.php file would look like this.

    Last, you would simply enter [page_sidebar id="4" ] (or whichever page ID you need) in the text widget of your sidebar.

    This is the result. Hope this was useful to you!

    Thread Starter Fi Fi P

    (@fi-fi-p)

    Thank you SO much, that is exactly what I was looking for!
    Much appreciated! x

    Wonderful! Glad it did the trick.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Insert Page image and title to a widget’ is closed to new replies.