简体   繁体   English

如何在Wordpress中显示有关帖子的简短描述?

[英]How to display a short description about a Post in Wordpress?

I get Post using Wp_query(). 我使用Wp_query()获得Post。 And then show post_thumbnail and the title. 然后显示post_thumbnail和标题。

<?php

$args = array(
    'type' => 'post',
    'category__in' => '23',
    'posts_per_page' => 1,
    'offset' => 2,
);

$lastBlog = new WP_Query($args);
if ($lastBlog->have_posts()):
    while ($lastBlog->have_posts()): $lastBlog->the_post();

        if (has_post_thumbnail()) {
            the_post_thumbnail();
            the_title(sprintf('<h4 class="entry-title"><a href="%s">', esc_url(get_permalink())), '</a></h4>');
        }

    endwhile;
endif;
wp_reset_postdata();
?> 

If I want to show short description about the post under the Title and insert Read More, how can I do that? 如果我想在标题下显示有关该帖子的简短说明,并插入更多信息,该怎么办?

Thanks 谢谢

There are two ways you could do that. 有两种方法可以做到这一点。 One way is to just output the content of the article, and put a 'Read more' tag in the text <!--more--> , and you'll get the text outputted with the 'Read more' button after it: 一种方法是仅输出文章的内容,并在文本<!--more-->放置“更多”标签,然后在其后使用“更多”按钮获取输出的文本:

<?php 
$args = array( 
    'type' => 'post',
    'category__in' => '23',
    'posts_per_page' => 1, 
    'offset' => 2,
    );

$lastBlog = new WP_Query( $args ); 

if( $lastBlog->have_posts() ):
    while( $lastBlog->have_posts() ): $lastBlog->the_post();
        if ( has_post_thumbnail() ) {  
    echo '<div class="post_wrapper">';
    the_post_thumbnail();
    the_title( sprintf('<h4 class="entry-title"><a href="%s">', esc_url( get_permalink() ) ),'</a></h4>' );
    the_content( 'Read more ...' );
    echo '</div>';
        }
    endwhile;
endif;

wp_reset_postdata();      
?> 

I've wrapped everything in .post_wrapper div, for easier handling. 我将所有内容包装在.post_wrapper div中,以便于处理。

The other way is to use the excerpt() with a manually added 'Read more' button 另一种方法是the excerpt()与手动添加的“阅读更多”按钮一起使用

<?php 
$args = array( 
    'type' => 'post',
    'category__in' => '23',
    'posts_per_page' => 1, 
    'offset' => 2,
    );

$lastBlog = new WP_Query( $args ); 

if( $lastBlog->have_posts() ):
    while( $lastBlog->have_posts() ): $lastBlog->the_post();
        if ( has_post_thumbnail() ) {  
    echo '<div class="post_wrapper">';
    the_post_thumbnail();
    the_title( sprintf('<h4 class="entry-title"><a href="%s">', esc_url( get_permalink() ) ),'</a></h4>' );
    the_excerpt();
    echo '<a href="'.esc_url(get_permalink()).'" class="read_more_button" title="'.esc_html__('Read more...', 'theme_slug').'">'.esc_html__('Read more...', 'theme_slug').'</a>';
    echo '</div>';
        }
    endwhile;
endif;

wp_reset_postdata();      
?> 

You can choose which one to use. 您可以选择使用哪一个。

Also are you sure you don't want to display the post if you don't have the thumbnail? 您还确定如果没有缩略图就不想显示帖子吗? if not, just move the if condition before the title, and you should have the post, even if you didn't set the post thumbnail. 如果不是,只需将if条件移动到标题之前,即使您没有设置帖子缩略图,也应该拥有该帖子。 If you meant it this way, then all is ok. 如果您是这样说的,那么一切都很好。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM