Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Replace deprecated code? WordPress

  • SOLVED

I am using the following code on a template to show multiple posts.

To show the multiple posts I just reuse the code and change the offset.

I like this code as I can just adjust the args and have it display a specific post where I need it.

The problem is that I keep getting error messages that this is deprecated code.

Please can someone provide better/up to date code?

Example code for 2 instances:


<div class="col-1-1">
<div class="content">
<?php query_posts('showposts=1'); ?>
<?php $posts = get_posts('category=-30,-133,-7&numberposts=1&offset=0'); foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h3><span class="latest-bold">Latest News:</span> <?php the_title(); ?></h3></a>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium', array('class' => 'thumbnail')); ?></a>

<?php $count1++; } ?>
<?php endforeach; ?>
</div>
</div>

<div class="col-1-2">
<div class="content">
<?php query_posts('showposts=1'); ?>
<?php $posts = get_posts('category=-30,-133,-7&numberposts=1&offset=1'); foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumb_m', array('class' => 'thumbnail')); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h4><?php the_title(); ?></h4></a>

<?php $count1++; } ?>
<?php endforeach; ?>
</div>
</div>

Answers (3)

2014-02-13

Hariprasad Vijayan answers:

Hello,

Let me know what error you are getting?

You can write it like this


<?php
// The Query
query_posts( 'cat=-30,-133,-7&posts_per_page=1&offset=0');

// The Loop
while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h3><span class="latest-bold">Latest News:</span>
<?php the_title(); ?>
</h3>
</a>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('medium', array('class' => 'thumbnail')); ?>
</a>
<?php endwhile;

// Reset Query
wp_reset_query();
?>

Let me know if you need any help in this

Thanks


julesphoto comments:

Thanks that did the trick!


Hariprasad Vijayan comments:

glad to hear


julesphoto comments:

That got rid of the error but I now get this feedback from Theme Mentor:

Usage of query_posts is discouraged, use a WP_Query instance instead. at file images/homepage.php, line 55: <?php query_posts('showposts=1'); ?>

How would I achieve the same with WP_Query?


Hariprasad Vijayan comments:

Try this

<?php
// WP_Query arguments
$args = array (
'cat' => '-30,-133,-7',
'posts_per_page' => '1',
'offset' => '0',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h3><span class="latest-bold">Latest News:</span>
<?php the_title(); ?>
</h3>
</a>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('medium', array('class' => 'thumbnail')); ?>
</a>
<?php
}
} else {
// no posts found
}

// Restore original Post Data
wp_reset_postdata();
?>


Hariprasad Vijayan comments:

Is it works for you?


julesphoto comments:

Perfect! Thanks!

2014-02-13

Arnav Joy answers:

you have following

<?php query_posts('showposts=1'); ?>

showposts is deprecated , you can use posts_per_page for it , as

<?php query_posts('posts_per_page=1'); ?>

2014-02-13

Yakir Sitbon answers:

Hey !
You are need to use with [[LINK href="http://codex.wordpress.org/Class_Reference/WP_Query"]]WP_Query[[/LINK]].
Do you want proper code?