admin管理员组文章数量:1327826
I'm searching to create a shortcode inside function.php child theme, to show last updated posts with thumbnail. I have this code that works into page template:
<ol class="list-numbered">
<?php
// Show recently modified posts
$recently_updated_posts = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => '13',
'orderby' => 'modified',
'no_found_rows' => 'true' // speed up query when we don't need pagination
) );
if ( $recently_updated_posts->have_posts() ) :
while( $recently_updated_posts->have_posts() ) : $recently_updated_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?>
<?php
$size = 'thumbnail';
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 1)
);
foreach ( $attachments as $thumb_id => $attachment ) {
echo wp_get_attachment_image($thumb_id, $size);
}
?>
</a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</ol>
But I would insert into functions.php file and call the shortcode. Do you have some ideas? Thanks in advance.
I'm searching to create a shortcode inside function.php child theme, to show last updated posts with thumbnail. I have this code that works into page template:
<ol class="list-numbered">
<?php
// Show recently modified posts
$recently_updated_posts = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => '13',
'orderby' => 'modified',
'no_found_rows' => 'true' // speed up query when we don't need pagination
) );
if ( $recently_updated_posts->have_posts() ) :
while( $recently_updated_posts->have_posts() ) : $recently_updated_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?>
<?php
$size = 'thumbnail';
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 1)
);
foreach ( $attachments as $thumb_id => $attachment ) {
echo wp_get_attachment_image($thumb_id, $size);
}
?>
</a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</ol>
But I would insert into functions.php file and call the shortcode. Do you have some ideas? Thanks in advance.
Share Improve this question asked Jul 4, 2020 at 16:35 FeliceAntonioFeliceAntonio 32 bronze badges 1- It's not clear what your question is here - are you asking how to create a shortcode with the working code you've got there? – mozboz Commented Jul 4, 2020 at 17:28
2 Answers
Reset to default 0You can do this like bellow
// register shortcode
add_shortcode('last-modified', 'last_mofied_shortcode_callback');
function last_mofied_shortcode_callback() {
$output = '<ol class="list-numbered">';
$recently_updated_posts = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => '13',
'orderby' => 'modified',
'no_found_rows' => 'true' // speed up query when we don't need pagination
) );
if ( $recently_updated_posts->have_posts() ) :
while( $recently_updated_posts->have_posts() ) : $recently_updated_posts->the_post();
$output .= '<li><a href="'.get_the_permalink().'" rel="bookmark" title="'.get_the_title().'">'.get_the_title();
$size = 'thumbnail';
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'numberposts' => 1));
foreach ( $attachments as $thumb_id => $attachment ) {
$output .= wp_get_attachment_image($thumb_id, $size);
}
$output .= '</a></li>';
endwhile;
wp_reset_postdata();
endif;
return $output;
}
And now you can use [last-modified]
shortcode anywhere within content. if you want to use in php files then just use it like echo do_shortcode('[last-modified]');
Read more about add_shortcode()
here
This code works well. I added category_name in $recently_updated_posts to show recently updated post for each category, and it works too. When I try to add get_category_link() and get_the_category() returns "Array" instead the category links and the category name. Where do I wrong? Thanks in advance.
本文标签: Last updated posts shortcode in functionsphp
版权声明:本文标题:Last updated posts shortcode in functions.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225334a2436208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论