doucitan2544 2016-01-31 22:16
浏览 659
已采纳

使用the_post_thumbnail()正确显示缩略图

I have created a custom post type named bateau in functions.php. I also created a page where all the posts that belong to the bateau custom post type are displayed, showing some of the most important fields. Finally, when I click on one of those posts, a link sends me to the related custom post page, i.e. a particular type of boat.

The custom post bateau has its own custom fields, a thumbnail, and also its custom taxonomies.

I want to retrieve and display, when I'm on the page of a particular boat, not only the most important but all its custom fields, its thumbnail and its taxonomy.

So, in the functions.php, I have written this filter :

add_filter('the_content','add_text');

    function add_text($text) {

        global $post;
        $text = "";

        if($post->post_type == 'bateau') { 

        $text.= "<h1 class=\"bateau-entry-title\">".get_post_meta( $post->ID, 'bateau_nom', true )."</h1>";
        return $text;
        }
    }   

It works fine, provided that I don't write plain HTML text within closing and opening PHP tags, i.e. it only works if I wrap all the HTML in a PHP text var. If I don't do so, the content is also displayed at the beginning of the header, not once, but twice. Strange, isn't it ?

If I add this line :

$text.= "<img class=\"thumb\" src=\"the_post_thumbnail();

the thumbnail displays properly in the "article"...but, guess what, also at the beginning of the header, not once, but twice !!! I just can't find why the thumbnail behaves like this. Anyone can help please ?

  • 写回答

2条回答 默认 最新

  • dongmi4927 2016-02-01 00:46
    关注

    In Wordpress, the page you called:

    when I'm on the page of a particular boat

    is called single. instead of putting a filter on the_content using

    add_filter('the_content','add_text');
    

    you would better add whatever you want to the loop. To do so, you may create a file called single-bateau.php (this is a protocol, so you need to name it exactly as this) in your current theme's root directory and there you may have your loop:

    if(have_posts()){
        while(have_posts()){
            the_post();
            the_title();
            if( has_post_thumbnail() ){
                the_post_thumbnail( $thumb_size );
            }
            the_taxonomies();
            the_category();
            the_tags();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站