【问题标题】:Modify Wordpress plugin to show posts by category instead of id#修改 Wordpress 插件以按类别而不是 id 显示帖子#
【发布时间】:2014-03-06 05:51:15
【问题描述】:

我正在使用这个 Wordpress 插件:http://wordpress.org/plugins/carousel-of-post-images/ 它显示一个帖子滑块并显示特色图像。 当前设置为按 id 显示帖子,这些帖子需要单独输入到短代码中。是否可以按类别显示帖子,以便我可以设置帖子的类别并在滑块中仅查看该类别?

这是我认为负责的所有代码。

function copi_carousel_get_images($size = 'medium' , $orderby, $posts, $count, $class = ''){
global $post;

$att_array = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'orderby' => $orderby, 
    'numberposts' => $count,
    );

$postlist = explode(",",$posts);
$sizes = explode(",", $size);
$html = '';
if (count($sizes) == 2)
{
    $width = $sizes[0];
    $height = $sizes[1];
    $size=$sizes;
}
else
{
    $width = get_option($size.'_size_w');
    $height = get_option($size.'_size_h');
}

foreach($postlist as $postid)
{
    if ($postid != '')
        $att_array['post_parent'] = $postid;

    $attachments = get_posts($att_array);

    if (is_array($attachments)){
        foreach($attachments as $att){
            $image_src_array = wp_get_attachment_image_src($att->ID, $size);
            $url = $image_src_array[0];
            if ($url != "")
            {

                $prefix = '<li><a href="'.get_permalink($att->post_parent).'">';
                $suffix = "</a></li>";

                $caption = $att->post_excerpt;

                if(function_exists('thumbGen'))
                    $url = thumbGen($url, $width, $height, 'return=1');

                $image_html = '<img src="%s" height="'.$height.'" alt="%s">';

                $html .= $prefix.sprintf($image_html,$url,$caption,$class).$suffix;
            }
        }
    }
}
return  $html;

}

function show_wp_copi_carousel($atts){
    $skin = 'tango';
    $div='post-carousel';
    $imagesize = 'medium';
    $orderby = 'rand';
    $postid = '';
    $count='10';

if (isset($atts['skin'])) 
    $skin = $atts['skin'];

if (isset($atts['imagesize'])) 
    $imagesize = $atts['imagesize'];

if (isset($atts['orderby'])) 
    $orderby = $atts['orderby'];

if (isset($atts['postid'])) 
    $postid = $atts['postid'];

if (isset($atts['count'])) 
{
    $count = $atts['count'];

【问题讨论】:

  • 您好,虽然很老了,但是这个帖子没有得到插件作者的回复:wordpress.org/support/topic/… 这表明这个功能在这个插件中并不容易实现。我建议寻找替代方案,而不是尝试重新编写此插件。
  • 谢谢,我查看了插件的支持论坛,最后一篇帖子已经 3 个月了,没有回复,这是一个相当简单的请求,让我相信开发人员不太重视支持。我可能是错的,但作者没有回复我并没有“暗示这个功能在这个插件中不容易实现”。如果是,那就这样吧,但我已经花了很多时间寻找没有运气的替代品,这个插件最接近我需要的。
  • 当然,有时插件的作者只是错过了帖子或由于其他原因不回复......但我在支持论坛上看了看,也看到了这个wordpress.org/support/topic/images-from-custom-post-type-cpt,这表明它并不意味着非常可定制。

标签: php jquery wordpress


【解决方案1】:

我用 Coda 滑块来做这个。

首先在头部包含脚本:

<script src="js/jquery.coda-slider-3.0.js"></script>

<script>
$(function(){

  /* Here is the slider using default settings */
  $('#slider-id').codaSlider({
        autoSlide:true,
        autoHeight:false
});

});

</script> 

然后我使用了循环:

<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>

<div class="coda-slider"  id="slider-id">
<?php
$args = array('category' => 15, 'numberposts' => 4, 'order'=> 'ASC');
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?>
<img src="<?php echo $image[0]; ?>"/>
<?php endforeach; ?>
</div>

这应该可行!

注意:要显示帖子信息,只需在循环中包含“the_post”或您要显示的任何内容,滑块脚本将负责旋转。

另外,这需要在模板中使用。

【讨论】:

  • 谢谢,这有点超出我的能力,但是,我已经制作了模板并且滑块正在运行,但我没有看到帖子。我不明白“只在循环中包含“the_post””去哪里,我不确定这如何引用帖子类别。我正在特别寻找一种在页面上拥有多个这些滑块的方法,每个滑块都引用一个特定的帖子类别。
  • @Cormac 我了解,如果您不熟悉模板或 PHP,wordpress“循环”可能会很棘手。您找到的任何插件都将利用其源代码中的循环来生成帖子或内容列表。随时给我发电子邮件,也许我可以帮助你更多。 buckmcgrane@gmail.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多