user2013488
user2013488

Reputation: 363

Plugin dynamic title using WordPress

I'm trying to make a small plugin for my blog (WordPress), but I have the following two problems.

  1. I want from the plugin to take the latest three posts from a custom category, but now it takes only the last one and duplicates it three times. How can I fix that?

  2. I want to make a dynamic title. That means that I want to be able to change the title of the plugin from the admin control panel. How can I accomplish that?

UPDATE:

Thanks to you people I managed to display the post image, but it doesn't displaying in the right spot.

This is the proper HTML

<li>
    <div class="projects">
        <ul class="projects sticker">
            <li><h2><?php the_title(); ?></h2></li>
            <li><p><a href="">details</a></p></li>
        </ul>
        <img src="" />
    </div>
</li>

This is how it's displaying now

<li>
    <div class="projects">
         <ul class="projects sticker">
             <li><h2><?php the_title(); ?></h2></li>
             <li><p><a href="">details</a></p></li>
         </ul>
   </div>

Basically I have to put the img tag inside the list and div.

Here is my code so far

$args = array('numberposts' => '3', 'category' => $cat_id);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent ) {
    echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look   '.esc_attr($recent["post_title"]).'" >'
    .'<li>' .'<div class="projects-wrapper">' .'<ul class="projects-sticker">'       .'<li>' .'<h2>' .   $recent["post_title"] .'</li>' .'</h2>' .'<li><p><a href="">details</a></p></li></ul>' .'<img src="'.the_post_thumbnail('thumbnail').'" />'  .'</div>' .'</li>'.'</a>';

Upvotes: 0

Views: 370

Answers (2)

Rikesh
Rikesh

Reputation: 26451

To get recent post better use wp_get_recent_posts. Here is the snippet of it.

$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a>';
    echo get_the_post_thumbnail($recent["ID"], 'thumbnail');
}
wp_reset_query();

Upvotes: 1

janw
janw

Reputation: 6662

Ordering posts

You use showposts => 3 That is not a valid arg use posts_per_page => 3, remove the numberposts. Also you order random: 'orderby' => 'rand' that has to be 'orderby' => 'date' More arguments can be found on the WP_Query page.

So use:

$args = array('posts_per_page' => 3, 'orderby' => 'date', 'category' => $cat_id);

Widget title

Check out the Widget API, extend the widget class and add your field in the form function. That way you can also make the number of posts variable for example.

Upvotes: 1

Related Questions