Shortcode to display "news-articals" custom post type







 // Shortcode to display "news-articals" custom post type

function shortcode_news_articals() {

    ob_start(); // Start output buffering

    // Query 

    $the_query = new WP_Query(array(

        'post_type'      => 'news-articals',

        'posts_per_page' => -1,

        'post_status'    => 'publish',

'meta_key'       => 'date',           // the ACF field name

'orderby'        => 'meta_value_num', // order by numeric value

'order'          => 'DESC',           // or 'ASC'

    ));


    if ( $the_query->have_posts() ) : ?>

        <div class="newsarticals">

        <?php while ( $the_query->have_posts() ) : $the_query->the_post();

$date = get_field( "date" );

$url = get_field( "url" );

?>

<div class="dat-artical">

<div class="date">

<h3><?php echo $date; ?></h3>

</div>

<div class="articals">

<h3><a href="<?php echo $url; ?>"><?php the_title(); ?></a></h3>

</div>

</div>

        <?php endwhile; ?>

        </div>

    <?php else : ?>

        <p>No news articles found.</p>

    <?php endif;


    wp_reset_postdata(); // Only this is needed


    return ob_get_clean(); // Return the output

}

add_shortcode('news_articals', 'shortcode_news_articals');

Comments

Popular posts from this blog

Get Post Data From Simple Post custom shortcode

Top 20 Beautiful Pen For Writing

Fatal error: Maximum execution time of 300 seconds exceeded in C:\xampp\phpMyAdmin\libraries\classes\Dbi\DbiMysqli.php on line 199