Forums

[resolved] shortcode to call latest post in category (8 posts)

  1. amysdaddy
    Member
    Posted 1 year ago #

    I need a shortcode that calls the most recent post of a particular category and places the content in a page. I have found similar shortcode that calls and shows a list of a set number of posts from a particular category, but my attempts at altering the code to call the content of a single, most recent post from a particular category did not work.

    Any thoughts? Help? Thanks.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    I have found similar shortcode that calls and shows a list of a set number of posts from a particular category

    could you post that code?
    or if it is longer han a few lines, could you paste it into a http://wordpress.pastebin.com/ and post the link to it here?

    working from some existing code is easier than re-inventing the wheel ...

  3. amysdaddy
    Member
    Posted 1 year ago #

    function sc_liste($atts, $content = null) {
    extract(shortcode_atts(array(
    "num" => '5',
    "cat" => ''
    ), $atts));
    global $post;
    $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
    $retour='

    ';
    return $retour;
    }
    add_shortcode("list", "sc_liste");
    -------------------------------
    shortcode example: [liste num="3" cat="1"]

  4. amysdaddy
    Member
    Posted 1 year ago #

    Better code layout:
    ----------------------------

    function sc_liste($atts, $content = null) {
            extract(shortcode_atts(array(
                    "num" => '5',
                    "cat" => ''
            ), $atts));
            global $post;
            $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
            $retour='<ul>';
            foreach($myposts as $post) :
                    setup_postdata($post);
                 $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
            endforeach;
            $retour.='</ul> ';
            return $retour;
    }
    add_shortcode("list", "sc_liste");<code></code>
  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    edit this bit:

    foreach($myposts as $post) :
                    setup_postdata($post);
                 $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
            endforeach;

    to include the content:

    foreach($myposts as $post) :
                    setup_postdata($post);
                 $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a><span>'.apply_filters('the_content',get_the_content().'<span></li>';
    
            endforeach;

    this would give you the title and the content.
    i am not totally sure if this will work with the formatting; i.e. how the result will look.

    (( for content only change the $retour line:

    $retour.='<li>'.apply_filters('the_content',get_the_content().'</li>';

    ))

  6. amysdaddy
    Member
    Posted 1 year ago #

    Thanks, alchymyth...

    When I use this code:

    function sc_liste($atts, $content = null) {
            extract(shortcode_atts(array(
                    "num" => '5',
                    "cat" => ''
            ), $atts));
            global $post;
            $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
            foreach($myposts as $post) :
                    setup_postdata($post);
                 $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a><span>'.apply_filters('the_content',get_the_content().'<span></li>';
            endforeach;
            return $retour;
    }
    add_shortcode("list", "sc_liste");

    I get this error:

    Parse error: syntax error, unexpected ';'

    Any thoughts?

  7. alchymyth
    The Sweeper
    Posted 1 year ago #

    my bad; missed a ) bracket after get_the_content() in my code (happens a lot when i just type things :-(

    this hopefully works:

    $retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a><span>'.apply_filters('the_content',get_the_content() ).'<span></li>';

  8. amysdaddy
    Member
    Posted 1 year ago #

    alchymyth-

    Thank you very much. The shortcode below works and allows me to place a link that calls a page showing the most recent post in a particular category. I took out the title call and the ul/li. The content shows up perfect under the page title.

    function sc_liste($atts, $content = null) {
            extract(shortcode_atts(array(
                    "num" => '5',
                    "cat" => ''
            ), $atts));
            global $post;
            $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
    
            foreach($myposts as $post) :
                    setup_postdata($post);
                 $retour.='<span>'.apply_filters('the_content',get_the_content() ).'<span>';
            endforeach;
    
            return $retour;
    }
    add_shortcode("list", "sc_liste");

    shortcode: [liste num="1" cat="#"]

    I sure do appreciate the wordpress community's willingness to share and help. Have a great day!

Topic Closed

This topic has been closed to new replies.

About this Topic