Simple News Shortcode

Below, you’ll find a detailed guide on how to add the Simple News Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Simple News Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Simple News Plugin and the shortcodes it provides:

Plugin Icon
Simple News

"Simple News is a user-friendly WordPress plugin designed to effortlessly integrate a news section into your website. It's perfect for keeping your audience updated on the latest happenings."

★★★★☆ (11) Active Installs: 2000+ Tested with: 6.1.4 PHP Version: false
Included Shortcodes:
  • [hjemmesider_news]

Simple News [hjemmesider_news] Shortcode

The Simple-News shortcode allows users to display news posts on any page. It has customizable attributes such as order, number, offset, category, and excerpt. It also supports different post types. The code initiates a query based on the defined attributes. For singular news posts, related news, or other pages/categories, it sets unique parameters. The loop runs based on this query, displaying the news posts in a specified format.

Shortcode: [hjemmesider_news]

Parameters

Here is a list of all possible hjemmesider_news shortcode parameters and attributes:

  • order – Sets the order of news posts (default is ‘order’)
  • number – Defines the number of news posts to display (default is 99999)
  • offset – Skips the specified number of news posts in the output (default is 0)
  • cat – Specifies the category of news posts to display (default is ‘cat’)
  • col – Determines the number of columns in the output (default is 0)
  • excerpt – Decides whether to show the excerpt of the news post (default is ‘yes’)
  • type – Sets the type of news post to display (default is ‘normal’)

Examples and Usage

Basic example – Display news posts in default order with unlimited number.

[news]

Advanced examples

Display the first 5 news posts in descending order.

[news order="DESC" number=5]

Display the first 5 news posts in descending order, excluding the first post.

[news order="DESC" number=5 offset=1]

Display the first 5 news posts from a specific category in descending order.

[news order="DESC" number=5 cat="specific-category"]

Display related news posts in descending order.

[news type="related"]

Display news posts in a specific column layout.

[news col=2]

Display news posts without the excerpt.

[news excerpt="no"]

PHP Function Code

In case you have difficulties debugging what causing issues with [hjemmesider_news] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('news', 'hjemmesider_news');

Shortcode PHP function:

function hjemmesider_news($atts) {
    global $post;
    ob_start();

// define attributes and their defaults
    extract(shortcode_atts(array('order' => 'order', 'number' => 99999, 'offset' => 0, 'cat' => 'cat', 'col' => 0, 'excerpt' => 'yes', 'type' => 'normal' ), $atts));


// define query parameters based on attributes

/* single post = news */
    if ( is_singular('news') ) {
        $options = array(
            'post_type' => 'news',
            'post__not_in' => array($post->ID),
            'order' => $order,
            'orderby' => 'date',
            'posts_per_page' => $number,
            'offset' => $offset,
            'cat' => array($cat));
    }

/* related news */
    if ( $type = 'related' ) {
        $options = array(
            'post_type' => 'news',
            'post__not_in' => array($post->ID),
            'category__in' => wp_get_post_categories( $post->ID ),
            'order' => $order,
            'orderby' => 'date',
            'posts_per_page' => $number,
            'offset' => $offset,
            'cat' => array($cat));
    }

/* pages, cat */
    else {
        $options = array(
            'post_type' => 'news',
            'order' => $order,
            'orderby' => 'date',
            'posts_per_page' => $number,
            'offset' => $offset,
            'cat' => array($cat));
    }

$the_query = new WP_Query($options);
$img_options = get_option( 'simple_news_settings' );

 // run the loop based on the query

    if ($the_query->have_posts()) {

        static $i = 1;
        $shortcode = 'sh-id-' . $i;

        echo '<div class="simple-news-con col-' . $col . ' ' . $shortcode . '">';
            while ($the_query->have_posts()): $the_query->the_post();
                simple_news_loop(); wp_reset_postdata();
            endwhile;
        echo '</div>';

    }

    $i++;
    $myvariable = ob_get_clean();
    return $myvariable;
}

Code file location:

simple-news/simple-news/files/shortcode.php

Conclusion

Now that you’ve learned how to embed the Simple News Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *