• I am upgrading an old WP page. and a part of it is to introduce widgets.
    Some of my previous code I want to present in your widget in order to simplify possible changes in a child-theme.

    I have this piece of code:

    <?php $args = array(
    	'posts_per_page'  => 20,
    	'offset'          => 0,
    	'category'        => 11,
    	'orderby'         => 'post_date',
    	'order'           => 'DESC',
    	'include'         => '',
    	'exclude'         => '',
    	'meta_key'        => '',
    	'meta_value'      => '',
    	'post_type'       => 'post',
    	'post_mime_type'  => '',
    	'post_parent'     => '',
    	'post_status'     => 'publish',
    	'suppress_filters' => true );
    
    $postslist = get_posts( $args );
    foreach ($postslist as $post) :  setup_postdata($post); ?>
    	<div>
    		<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(thumbnail); } ?>
    	</br>
    		<?php the_title(); ?></a>
    	</br></br>
    	</div>
    <?php endforeach; ?>

    it is working in my old theme, showing 20 posts thumbs and headlines. I am using it to present my category “photo album” in sidebar.

    the code is working, when it is hard-coded into sidebar.php, but when it is placed in your widget plugin – it doesn’t (it just shows the same post 20-times)

    What is wrong?

    https://www.ads-software.com/plugins/php-code-widget/

Viewing 1 replies (of 1 total)
  • Plugin Author Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Probably because your code is assuming the global $post, but no real way to tell. Try adding a global $post to the top of it.

Viewing 1 replies (of 1 total)
  • The topic ‘my php code works and it doesn't’ is closed to new replies.