有没有简短的代码可以用来获取带有缩略图插件的帖子列表?

时间:2011-06-01 作者:Ramkumar M

任何可用的短代码Get Post List With Thumbnail 插件?或者如何为这个特定的插件创建短代码?提前谢谢你。

3 个回复
最合适的回答,由SO网友:Bainternet 整理而成

这里有一个快速破解方法,可以将此代码粘贴到主题函数中,使其成为一个快捷代码。php文件:

add_shortcode(\'gplt\',\'getPostListThumbs_shortcode\');
function getPostListThumbs_shortcode($atts, $content = null){

    extract(shortcode_atts(array(
        \'orient\' => \'v\',
        \'imgo\' => false,
        \'ttgo\' => false,
        \'dtgo\' => false,
        \'dtfrm\' => 1,
        \'categ\' => \'\',
        \'postnr\' => 20,
        \'linn\' => 3,
        \'tbwid\' => 40,
        \'tbhig\' => 40       
    ), $atts));
    $orient = gtpartrat($orient,\'v\');
    $imgo = gtpartrat($imgo,false);
    $ttgo = gtpartrat($ttgo,false);
    $dtgo = gtpartrat($dtgo,false);
    $dtfrm = gtpartrat($dtfrm,1);
    $categ = gtpartrat($categ,\'\');
    $postnr = gtpartrat($postnr,20);
    $linn = gtpartrat($linn,3);
    $tbwid = gtpartrat($tbwid,40);
    $tbhig = gtpartrat($tbhig,40);

    $htmlcod = "<table id=\'div_postlist\' width=\'".$divwid."\' cellpadding=\'4\' cellspacing=\'4\'>"."\\n";
    $htmlcod .= "<tr>"."\\n";
    //
    if (have_posts()) :
        global $post;

        if($categ!=\'\'){
            $strquery = "numberposts=".$postnr."&category_name=". $categ;
        }
        else{
              $strquery =  "numberposts=".$postnr;
        }

        $myposts=get_posts($strquery);

        $ctxtr = 0;
        switch($dtfrm){
            case 1:
                $dtdis = \'d/m/y\';
            break;
            case 2:
                $dtdis = \'m/d/y\';
            break;
        }
        if($myposts):

        foreach($myposts as $post) :

                    $args = array(
                        \'post_type\' => \'attachment\',
                        \'numberposts\' => -1,
                        \'post_status\' => null,
                        \'post_parent\' => $post->ID
                    );
                    $attachments = get_posts($args);


                    $imgsrc = "";
                    if ($attachments):

                        foreach ($attachments as $attachment) {

                            $imgsrc = wp_get_attachment_image($attachment->ID, array($tbwid,$tbhig), $icon = false);
                            break;
                        }
                    endif;

                    if($orient=="v"){     
                            if($ctxtr == 0){
                                $htmlcod .= "<tr>"."\\n";
                            } 
                            $ctxtr = $ctxtr + 1;

                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                            if($imgsrc!=""):                           
                                $htmlcod .= "<a href=\'". get_permalink() ."\' title=\'". get_the_title() ."\'>"."\\n";
                                $htmlcod .= $imgsrc;
                                $htmlcod .= "</a>"."\\n";                           
                            endif;
                                $htmlcod .= "</td>"."\\n";

                            if(!$imgo){           
                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                                if($dtgo):
                                $htmlcod .= "<p>".get_the_time($dtdis)."</p>";
                                endif;
                                if($ttgo):
                                $htmlcod .= "<a href=\'". get_permalink()."\' title=\'". get_the_title() ."\'>";
                                $htmlcod .= get_the_title();
                                $htmlcod .= "</a>"."\\n";
                                endif;
                                $htmlcod .= "</td>"."\\n";
                            }
                            $htmlcod .= "</tr><tr>"."\\n";  
                    }
                    else{

                            if($ctxtr == 0){
                                $htmlcod .= "<tr>"."\\n";
                            }      
                            $ctxtr = $ctxtr + 1;                      
                            $htmlcod .= "<td valign=\'top\'>"."\\n";
                            $htmlcod .= "<table cellpadding=\'3\' cellspacing=\'3\' border=\'0\' width=\'100%\'>"."\\n";

                                $htmlcod .= "<tr>"."\\n";
                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                            if($imgsrc!=""):                           
                                $htmlcod .= "<a href=\'". get_permalink() ."\' title=\'". get_the_title() ."\'>"."\\n";
                                $htmlcod .= $imgsrc;
                                $htmlcod .= "</a>"."\\n";    
                                $htmlcod .= "</td>"."\\n";                       
                            endif;

                            if(!$imgo){  
                                $htmlcod .= "<td valign=\'top\'>"."\\n";             
                                if($dtgo):
                                $htmlcod .= "<p>".get_the_time($dtdis)."</p>";
                                endif;
                                if($ttgo):
                                $htmlcod .= "<a href=\'". get_permalink()."\' title=\'". get_the_title() ."\'>";
                                $htmlcod .= get_the_title();
                                $htmlcod .= "</a>"."\\n";
                                endif;      
                                $htmlcod .= "</td>"."\\n";                         
                            }
                            $htmlcod .= "</td>"."\\n";
                            $htmlcod .= "</tr>"."\\n";
                            $htmlcod .= "</table>"."\\n";
                            $htmlcod .= "</td>"."\\n";
                            if($ctxtr == $linn){
                                $htmlcod .= "</tr>"."\\n";
                                $ctxtr = 0;
                            }
                    }
        endforeach;
        else:
            $htmlcod = "<tr>"."\\n";
            $htmlcod = "<td>"."\\n";
            $htmlcod .= "No registers found."."\\n";
            $htmlcod .= "</td>"."\\n";
            $htmlcod .= "</tr>"."\\n";
        endif;
    endif;
        $htmlcod .= "</table>";
    return $htmlcod;
}

Usage:

只需调用短代码[gplt parametes] 从任何帖子或页面

parameters:

<方向:orient="v" // v=垂直,h=水平默认值“v”(仅显示图像):imgo="false" //true、false默认为“false”
  • 显示帖子标题:ttgo="false" //true、false默认值“false”显示发布日期:dtgo="false" //true、false默认“false”日期格式:dtfrm="1" // 1-年/月/日,2-年/月/日默认为“1”
  • 类别名称:categ=\'\' // 所有类别的保留或空白默认为“”
  • 帖子数量:postnr="20"//默认值=20每行寄存器数:linn="3" //默认值=3仅用于水平方向缩略图宽度:tbwid="40"//默认值=40缩略图高度:tbhig="40"//默认值=40

  • SO网友:Alvaro Neto

    我开发了一个新版本Get Post List With Thumbnails 4.0.0 现在可以使用短代码了。

    SO网友:Ramkumar M

        //Shortcode for Get Recent Post Thumbnail
    add_shortcode(\'gplt\',\'getPostListThumbs_shortcode\');
    
    函数getPostListThumbs\\u shortcode($atts,$content=null){

    extract(shortcode_atts(array(
        \'orient\' => \'v\',
        \'imgo\' => true,
        \'ttgo\' => true,
        \'dtgo\' => false,
        \'dtfrm\' => 1,
        \'categ\' => \'\',
        \'postnr\' => 500,
        \'linn\' => 3,
        \'tbwid\' => 175,
        \'tbhig\' => 100       
    ), $atts));
    $orient = gtpartrat($orient,\'v\');
    $imgo = gtpartrat($imgo,false);
    $ttgo = gtpartrat($ttgo,false);
    $dtgo = gtpartrat($dtgo,false);
    $dtfrm = gtpartrat($dtfrm,1);
    $categ = gtpartrat($categ,\'\');
    $postnr = gtpartrat($postnr,500);
    $linn = gtpartrat($linn,3);
    $tbwid = gtpartrat($tbwid,175);
    $tbhig = gtpartrat($tbhig,100);
    
    $htmlcod = "<table id=\'div_postlist\' width=\'".$divwid."\' cellpadding=\'4\' cellspacing=\'4\'>"."\\n";
    $htmlcod .= "<tr>"."\\n";
    //
    
    //if(have\\u posts()):全局$post;

        if($categ!=\'\'){
            $strquery = "numberposts=".$postnr."&category_name=". $categ;
        }
        else{
              $strquery =  "numberposts=".$postnr;
        }
    
        $myposts=get_posts($strquery);
    
        $ctxtr = 0;
        switch($dtfrm){
            case 1:
                $dtdis = \'d/m/y\';
            break;
            case 2:
                $dtdis = \'m/d/y\';
            break;
        }
        if($myposts):
    
        foreach($myposts as $post) :
    
                    $args = array(
                        \'post_type\' => \'attachment\',
                        \'numberposts\' => -1,
                        \'post_status\' => null,
                        \'post_parent\' => $post->ID
                    );
                    $attachments = get_posts($args);
    
    
                    $imgsrc = "";
                    if ($attachments):
    
                        foreach ($attachments as $attachment) {
    
                            $imgsrc = wp_get_attachment_image($attachment->ID, array($tbwid,$tbhig), $icon = false);
                            break;
                        }
                    endif;
    
                    if($orient=="v"){     
                            if($ctxtr == 0){
                                $htmlcod .= "<tr>"."\\n";
                            } 
                            $ctxtr = $ctxtr + 1;
    
                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                            if($imgsrc!=""):                           
                                $htmlcod .= "<a href=\'". get_permalink() ."\' title=\'". get_the_title() ."\'>"."\\n";
                                $htmlcod .= $imgsrc;
                                $htmlcod .= "</a>"."\\n";                           
                            endif;
                                $htmlcod .= "</td>"."\\n";
    
                            if(!$imgo){           
                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                                if($dtgo):
                                $htmlcod .= "<p>".get_the_time($dtdis)."</p>";
                                endif;
                                if($ttgo):
                                $htmlcod .= "<a href=\'". get_permalink()."\' title=\'". get_the_title() ."\'>";
                                $htmlcod .= get_the_title();
                                $htmlcod .= "</a>"."\\n";
                                endif;
                                $htmlcod .= "</td>"."\\n";
                            }
                            $htmlcod .= "</tr><tr>"."\\n";  
                    }
                    else{
    
                            if($ctxtr == 0){
                                $htmlcod .= "<tr>"."\\n";
                            }      
                            $ctxtr = $ctxtr + 1;                      
                            $htmlcod .= "<td valign=\'top\'>"."\\n";
                            $htmlcod .= "<table cellpadding=\'3\' cellspacing=\'3\' border=\'0\' width=\'100%\'>"."\\n";
    
                                $htmlcod .= "<tr>"."\\n";
                                $htmlcod .= "<td valign=\'top\'>"."\\n";
                            if($imgsrc!=""):                           
                                $htmlcod .= "<a href=\'". get_permalink() ."\' title=\'". get_the_title() ."\'>"."\\n";
                                $htmlcod .= $imgsrc;
                                $htmlcod .= "</a>"."\\n";    
                                $htmlcod .= "</td>"."\\n";                       
                            endif;
    
                            if(!$imgo){  
                                $htmlcod .= "<td valign=\'top\'>"."\\n";             
                                if($dtgo):
                                $htmlcod .= "<p>".get_the_time($dtdis)."</p>";
                                endif;
                                if($ttgo):
                                $htmlcod .= "<a href=\'". get_permalink()."\' title=\'". get_the_title() ."\'>";
                                $htmlcod .= get_the_title();
                                $htmlcod .= "</a>"."\\n";
                                endif;      
                                $htmlcod .= "</td>"."\\n";                         
                            }
                            $htmlcod .= "</td>"."\\n";
                            $htmlcod .= "</tr>"."\\n";
                            $htmlcod .= "</table>"."\\n";
                            $htmlcod .= "</td>"."\\n";
                            if($ctxtr == $linn){
                                $htmlcod .= "</tr>"."\\n";
                                $ctxtr = 0;
                            }
                    }
        endforeach;
        else:
            $htmlcod = "<tr>"."\\n";
            $htmlcod = "<td>"."\\n";
            $htmlcod .= "No registers found."."\\n";
            $htmlcod .= "</td>"."\\n";
            $htmlcod .= "</tr>"."\\n";
        endif;
    // endif;
        $htmlcod .= "</table>";
    return $htmlcod;
    
    }我在这行“if(have\\u posts()):”中发现了错误。我们需要制作评论行。1//如果(have\\u posts()):2。/endif;现在它工作得很好。万岁啊。感谢Bainternet:)

    结束

    相关推荐

    Shortcode empty attribute

    有没有办法为短代码创建一个空属性?示例:function paragraph_shortcode( $atts, $content = null ) { return \'<p class=\"super-p\">\' . do_shortcode($content) . \'</p>\'; } add_shortcode(\'paragraph\', \'paragraph_shortcode\'); 用户类型某物它显