dongse7261 2013-03-04 13:54
浏览 27

通过wordpress中的帖子附件获取每个类别的最新帖子

On the homepage of a wordpress install, I would like to display the latest post from each category with the category title, post attachment, post title, post excerpt, and post date.

The post attachment will always be a thumbnail version of the "featured image". The reason I wish to use the attachment and not the main image linked to the article, is we are using a plugin that alloows the editor to post a youtube link which overrides the featured image, so if I use "get_the_post_thumbnail" it pulls in the video instead of the image.

The code I am currently using from a mash up of solutions I have found is as follows:

        <?php


         query_posts('cat=3&showposts=1');
          if(have_posts()) :
            the_post();
            $images = get_children(array(
              'post_parent' => get_the_id(),
              'post_type' => 'attachment',
              'post_mime_type' => 'image',
              'orderby' => 'ID',
              'order' => 'ASC',
              'numberposts' => 1
            ));
            foreach((array)$images as $key => $image){
              echo wp_get_attachment_image($key);
              echo "<a href='" . get_permalink() . "'>Link</a>";
            }
            the_excerpt();
          endif;
        wp_reset_query();

    ?>

My problems with the above are as follows:

1) I cannot figure out how to get the category name (I could just hard code this and repeat the above for each category, but that seems like cheating).

2) When I repeat this code for the next category the image or permalink do not show up. I assume this is because I am repeating references or something like that, but I am too amateur with Wordpress to know.

Any help will be much appreciated. The final result is to look like this : http://www.tagdesignuk.com/lovethat/

Regards,

Kevin

UPDATE:

I have simplified this now, but still am a little stuck. I have removed the video plugin as I will find an easier way to do this later on, so now my only problem is pulling in the correct sized thumbnail.

I am using the following code:

<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=1' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
<?php echo get_the_post_thumbnail(); ?>
<?php the_content(); ?>
<?php the_date(); ?>
<?php endwhile; ?>

Any ideas on how to change the thumbnail size? The documentation (http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail) says to use as follows :

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

but in the absense of a post ID it doesnt work... and I do not know how to format it correctly and cant find any reference to what I should be doing.

Many thanks,

Kevin

  • 写回答

1条回答 默认 最新

  • dpleylxzx47207117 2013-03-04 14:25
    关注

    you can query for all the categories with get_categories() and then use the name property. For examples you should look at the Wordpress Codex entry for this function.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题