最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Help with a function to create a shortcode

programmeradmin0浏览0评论

I have this code that I use to show random posts that are cached using transients (obtained from here). I want to show it using a shortcode in a footer widget. Could anyone kindly help me with a function to create the shortcode out of this code? Thank you in advance.

<div class="gridcontainer">
<div class="widget-title">Trending Picks</div>
<?php
// Get any existing copy of our transient data
if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
    // It wasn't there, so regenerate the data and save the transient
    $randargs = array('orderby' => 'rand', 'numberposts' => 20);
    $special_query_results = get_posts($randargs);
    set_transient( 'special_query_results', $special_query_results, 60*60*12 );
}

// Use the data like you would have normally...
$randomposts = get_transient( 'special_query_results' );
$randkey = array_rand( $randomposts, 5 );
$sixposts[0] = $randomposts[$randkey[0]];
$sixposts[1] = $randomposts[$randkey[1]];
$sixposts[2] = $randomposts[$randkey[2]];
$sixposts[3] = $randomposts[$randkey[3]];
$sixposts[4] = $randomposts[$randkey[4]];
$sixposts[5] = $randomposts[$randkey[5]];

global $post;
foreach( $sixposts as $post ) :  setup_postdata($post); ?>


<div class="gridcontent">
<div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
</div>
<?php endforeach; ?>
    </div>

I have this code that I use to show random posts that are cached using transients (obtained from here). I want to show it using a shortcode in a footer widget. Could anyone kindly help me with a function to create the shortcode out of this code? Thank you in advance.

<div class="gridcontainer">
<div class="widget-title">Trending Picks</div>
<?php
// Get any existing copy of our transient data
if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
    // It wasn't there, so regenerate the data and save the transient
    $randargs = array('orderby' => 'rand', 'numberposts' => 20);
    $special_query_results = get_posts($randargs);
    set_transient( 'special_query_results', $special_query_results, 60*60*12 );
}

// Use the data like you would have normally...
$randomposts = get_transient( 'special_query_results' );
$randkey = array_rand( $randomposts, 5 );
$sixposts[0] = $randomposts[$randkey[0]];
$sixposts[1] = $randomposts[$randkey[1]];
$sixposts[2] = $randomposts[$randkey[2]];
$sixposts[3] = $randomposts[$randkey[3]];
$sixposts[4] = $randomposts[$randkey[4]];
$sixposts[5] = $randomposts[$randkey[5]];

global $post;
foreach( $sixposts as $post ) :  setup_postdata($post); ?>


<div class="gridcontent">
<div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
</div>
<?php endforeach; ?>
    </div>
Share Improve this question edited May 3, 2019 at 20:20 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 3, 2019 at 20:03 user145284user145284
Add a comment  | 

1 Answer 1

Reset to default 0

You want add_shortcode(). Read more about add_shortcode() in the Codex.

发布评论

评论列表(0)

  1. 暂无评论