WordPress.org

Forums

Customizr
Last post in slider? (4 posts)

  1. marcowijn
    Member
    Posted 1 year ago #

    How can I get my latest post in slider front page?

    regards,
    Marco

  2. d4z_c0nf
    Member
    Posted 1 year ago #

    Your latest post or your latest posts? And in the slider replacing the image or what?

  3. ElectricFeet
    Member
    Posted 1 year ago #

    Well, it took a while but here you go:

    // Remap a slide's button to the latest post
    add_filter('tc_slide_link_url' , 'my_slide_custom_link', 10, 2);
    function my_slide_custom_link( $slide_link , $image_id ) {
    
        // Do nothing if the slide's image id is not the targeted one
        // CHANGE THIS NUMBER TO YOUR SLIDE IMAGE NUMBER
        if ( 489 != $image_id )
            return $slide_link;
    
        // Get the first of the recent posts ("reset" picks the first post in the array of posts)
        $myLatestPost = reset(wp_get_recent_posts( array('numberposts' => '1') ) );
    
        // Get its permalink, based on ID
    	$myLatestPostURL = get_permalink($myLatestPost["ID"]);
    
        //set a custom url for the targeted slide
        return $myLatestPostURL;
    }

    This drew heavily on this post of nikeo's, which showed how to map a whole slide or just the button to an external link.

    You need to change the image ID number to the one that corresponds to your slide. Nikeo used a a picture that shows you how to find the id of the image.

    I'm not a php nor WordPress expert, so there may be better ways of coding this.

    Let us know how you get on!

  4. ElectricFeet
    Member
    Posted 1 year ago #

    d4z_c0nf has pointed out a better way (thanks!), which avoids using the php reset function, as it doesn't work in some cases. Try this:

    // Remap a slide's button to the latest post
    add_filter('tc_slide_link_url' , 'my_slide_custom_link', 10, 2);
    function my_slide_custom_link( $slide_link , $image_id ) {
    
        // Do nothing if the slide's image id is not the targeted one
        // CHANGE THIS NUMBER TO YOUR SLIDE IMAGE NUMBER
        if ( 489 != $image_id )
            return $slide_link;
    
        // Get the first of the recent posts ("reset" picks the first post in the array of posts)
        $myLatestPost = wp_get_recent_posts( array('numberposts' => '1') )[0];
    
        // Get its permalink, based on ID
    	$myLatestPostURL = get_permalink($myLatestPost["ID"]);
    
        //set a custom url for the targeted slide
        return $myLatestPostURL;
    }

    See the notes above on how to use.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic

Tags

No tags yet.