WordPress'de her gönderinin altında 3 rastgele gönderi nasıl gösterilir?

  

4
Konu başlatıcı

WordPress sitem var ve her makalenin (gönderinin) altında 3 rastgele makale (gönderi) göstermek istiyorum, böylece tüm gönderilerimi birbirine bağlayabilirim. Bunun basit bir kod olmasını istiyorum - sadece tıklanabilir bir metin olarak gösterilsin - süslü bir şey değil ...

Lütfen yardım edebilir misiniz?

Belki functions.php'de bazı kodlar vardır?

Teşekkürler

2 Yanıt
3

functions.php'deki Çocuk temanıza şu kod satırlarını ekleyin:

/* Shortcode for random posts */
/* The shortcode to put in any post/widget is [my-random-posts] */
function my_rand_posts() {
 
    $args = array(
        'post_type'      => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => 3,
    );
 
    $the_query = new WP_Query($args);
    $string = "";
    if ($the_query->have_posts()) {
        $string .= '<ul>';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            $string .= '<li><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a></li>';
        }
        $string .= '</ul>';
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        $string .= 'no posts found';
    }
 
    return $string;
}
 
add_shortcode('my-random-posts', 'my_rand_posts');
add_filter('widget_text', 'do_shortcode');

Bu kod, istediğiniz zaman koyabileceğiniz bir kısa kod oluşturacaktır! #8 satırında (posts_per_page) kaç gönderinin gösterileceğini seçebilirsiniz - bizim durumumuzda: 3. 

Herhangi bir yazıya/widget'a yerleştirilecek kısa kod şudur: [my-random-posts]

İyi eğlenceler!

2

Ayrıca kendi WP eklentinizi de oluşturabilirsiniz. kodu ve kısa kodu saklamak için WordPress temanızı değiştirmeniz/değiştirmeniz durumunda.

İşte kısa kodu kullanarak son 3 gönderiyi (sondaki tarihlerle birlikte 🙂 gösteren WordPress eklentimin kodu: [my-plugin-recent-posts]:

<?php
 
/*
Plugin Name: My plugin recent posts shortcode
Plugin URI:  https://mypluginrecentpostsshortcode.com 
Description: WP plugin for showing recent posts with a shortcode
Author: My plugin recent posts shortcode Ltd.
Version: 1.0
Author URI:  https://mypluginrecentpostsshortcode.com 
Text Domain: my-plugin-recent-posts-shortcode
Domain Path: /languages/
 */
 
function my_plugin_recent_posts_shortcode() {
    $buffer = '<h4>Recent Posts:</h4><ul>';
 
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 3,
    );
 
    $q = new WP_Query( $args );
 
    while ( $q->have_posts() ) {
        $q->the_post();
        $buffer .= '<li><a href=" ' . get_the_permalink() . '">' . get_the_title() . '</a> - ' . get_the_date() . ' </li>';
    }
    wp_reset_postdata();
 
    $buffer .= '</ul>';
 
    return $buffer;
}
 
add_shortcode( 'my-plugin-recent-posts', 'my_plugin_recent_posts_shortcode' );

...ve bu da WordPress blogumun ekran görüntüsü:

wordpress eklentim son gönderiler kısa kodu

Paylaş: