SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Looking for a plugin/code that can retrieve the featured images from a category.

    Here is the website ariadneswonderland.gr/
    On the homepage I'm using http://wordpress.org/extend/plugins/...dow-boxes/faq/ a drop shadow box plugin
    under the gallery to show a series of images of featured posts, as of right now, as i'm still building the website so it's just lucky luke images inserted from media. What i'd like is to be able to do is have some code that pulls the 5 lastest posts from a category (in this case frontpage id 7) and have their images automatically placed inside the shortcode. Instead of having to retrieve and add links every time i want to change a feature.

    Any ideas/code/snipets/plugins that you might feel would work would be greatly appreciated.
    Last edited by Mittineague; Jul 26, 2012 at 15:04. Reason: fixing links

  2. #2
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with a customization, Looking for help with code that can retrieve featured img.

    Here is the website http://ariadneswonderland.gr/ that all this conserns.

    I dislike people that beg for help without having searched for a solution and i've done the best i could before coming here
    My code doesn't work, it might not even be close to correct but it was the best i could come up with.

    On the homepage I'm using a Drop shadow box plugin
    under the gallery to show a series of images of featured posts, as of right now, as i'm still building the website all you see is the White box under the gallery and a emoticon inside it.. What i'd like is to be able to do is have some code that pulls the 5 lastest posts from a category (in this case frontpage id 7) and Displays the Featured Post Image next to each other.

    Code:
    ==============================
    =       X             X              X             X            X  =
    ==============================
    Where X is a Featured post image automatically retrieved instead of having to retrieve and add links every time i want to change a feature.
    = is the automatically generated [dropshadowbox].

    Any ideas/code/snipets/plugins that you might feel would work would be greatly appreciated.

    So far however all i've come up with is this but i can't figure out how to implement the php into the post, phpexec and similar plugins don't seem to work for this, tho simple things like echo'test'; work fine. And making it a function doesn't work as it doesn't seem to return anything :S Obviously i need alot more coding experience but as of right now i'd like to just be done with this website


    PHP Code:
    // fetching latest posts from specific category
    $categoryId 7;
    $args = array('category' =>  categoryId  'post_status' => 'publish''numberposts' => 5);
    $posts get_posts$args );
    foreach(
    $posts as $post){
      
    $feat_image_url wp_get_attachment_urlget_post_thumbnail_id($post->ID) );
     
    $post_url get_permalink($post->ID);

    Last edited by TechnoBear; Jan 7, 2013 at 05:56. Reason: Threads merged.

  3. #3
    SitePoint Zealot bronze trophy
    Join Date
    Jan 2012
    Posts
    116
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by presdec View Post

    PHP Code:
    // fetching latest posts from specific category
    $categoryId 7;
    $args = array('category' =>  categoryId  'post_status' => 'publish''numberposts' => 5);
    $posts get_posts$args );
    foreach(
    $posts as $post){
      
    $feat_image_url wp_get_attachment_urlget_post_thumbnail_id($post->ID) );
     
    $post_url get_permalink($post->ID);

    In your $args array, you need a dollar sign in front of categoryID.

    For debugging, you might try throwing some print_r statements in there, or die() statements to see where you're at along the way, and where your function might be failing. Maybe some tryies and catches.

    A real simple thing might be to place something like this after your $posts variable definition: die(print_r($posts));

    And if that is returning an array of posts, then you know your code is working up to that point. Then you can move on to the next step and try something in your freach loop on the posts:

    PHP Code:
    foreach ($posts as $post) {
         
    print_r($post);

    And see what that turns up. That would give you an idea as to whether something might be failing at that point, like if there is an empty post object being returned or something like that. You might also try throwing some "if" statements in there to make sure your code isn't terminally failing due to an error, like an undefined or empty variable. EXAMPLE:

    PHP Code:
    foreach ($posts as $post) {
         if (!empty(
    $post->ID)) {
            
    $feat_image_url wp_get_attachment_urlget_post_thumbnail_id($post->ID) );
            
    $post_url get_permalink($post->ID); 
        }

    This way if one of your elements in the $posts array is all janky for whatever reason and isn't returning a valid post ID, then at least your covered from any fatal errors or warnings that might otherwise break the output or result of what you're trying to get from your code.

    Then of course you'd want to add a little echo statement there inside your foreach loop like this:

    echo '<a href="'.$post_url.'">';
    echo '<img src='.$feat_image_url.'" /></a>';


    Or however you might want to do it.

  4. #4
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks for the help

    After some struggling, debugging like mad i managed to get a working code:

    PHP Code:
    $categoryId = 7;
    $args = array('category' =>  $categoryId  , 'post_status' => 'publish', 'numberposts' => 5);
    $posters = get_posts( $args );
    if ( $posters ) {
    foreach ($posters as $post) { 

    $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
    ?>
            <a href="<?php echo get_permalink($post->ID); ?>" title=""><img src="<?php echo $imgsrc[0]; ?>" class="thumbnail" width="130" height="130" alt='' /></a>

        <?php }
    }
    I've made it into a shortcode [featuredposts category=7] and calling it lets me add the featured images of the latest 5 posts for a category of my choosing!

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $categoryId = 7;
    $args = array('category' => $categoryId , 'post_status' => 'publish', 'numberposts' => 5);
    $posters = get_posts( $args );
    if ( $posters ) {
    foreach ($posters as $post) {

    $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full");
    ?>
    <a href="<?php echo get_permalink($post->ID); ?>" title="<?php echo get_the_title($post->ID); ?>"><img src="<?php echo $imgsrc[0]; ?>" class="thumbnail" style="border-radius: 10px;" width="130" height="130" alt='' /></a>
    <?php }
    }
    Added a link to the post when u click image, and rounded corners.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •