【问题标题】:Show featured image from shortcode - Currently only have title and excerpt显示短代码的特色图片 - 目前只有标题和摘录
【发布时间】:2017-02-17 05:03:01
【问题描述】:

我目前有一个从 WPBeginner 提取的函数,它允许我创建一个短代码,可以将网站上的粘贴帖子拉到我想要的任何位置。

我也需要这个来显示特色图片,但不知道目前如何让它工作。当前的简码如下:

function wpb_latest_sticky() { 

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 1 );

/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
// The Loop
if ( $the_query->have_posts() ) {
    $return .= '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';

    }
    $return .= '</ul>';

} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

return $return; 

} 
add_shortcode('latest_stickies', 'wpb_latest_sticky');

这就是我在帖子页面上拉它的方式:

 <?php if ( has_post_thumbnail() && !is_search() ) { ?>
            <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( esc_html__( 'Permalink to ', 'quark' ) . '%s', the_title_attribute( 'echo=0' ) ) ); ?>">
                <?php the_post_thumbnail( 'post_feature_full_width' ); ?>
            </a>

任何帮助将不胜感激!

【问题讨论】:

  • 你试过get_the_post_thumbnail()

标签: wordpress


【解决方案1】:
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    $return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt() . '<br/>' . get_the_post_thumbnail() . '</li>';

}

【讨论】:

  • 这很好用,Jim 的回答给了我一个 500 错误,但 Andy 似乎已经修复了它。是否可以在标题、摘录和特色帖子中添加类以便于进行 css 编辑?非常感谢你们的帮助。
  • 你可以这样做来添加类:&lt;li&gt;&lt;a class="yourTitleClass" href="' .get_permalink(). '" title="' . get_the_title() . '"&gt;' . get_the_title() . '&lt;/a&gt;&lt;br /&gt;&lt;span="yourExcerptClass"&gt;' . get_the_excerpt() . '&lt;/span&gt;&lt;br/&gt;' . get_the_post_thumbnail('$post-&gt;ID', 'full', array( 'class' =&gt; 'yourImageClass') . '&lt;/li&gt;
  • 0 反对票如下:get_the_post_thumbnail( $_post->ID, 'thumbnail', array( 'class' => 'alignleft' ) );
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 2013-11-20
  • 2016-10-25
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多