Display google news in wordpress

There are several ways to display Google News in a WordPress website. Here are a few methods:

Method 1: Using a Plugin

  1. Install and activate a plugin like "Google News" or "News Widget" from the WordPress plugin directory.
  2. Configure the plugin settings to fetch news from Google News.
  3. Add the widget to your WordPress sidebar or footer.

Method 2: Using a Shortcode

  1. Install and activate a plugin like "Shortcode Ultimate" or "WPBakeryPageBuilder".

  2. Create a new shortcode by going to the plugin settings and adding a new shortcode.

  3. Use the following code to fetch Google News:

    [google-news query="your_query" num="5"]

    Replace "your_query" with the search query you want to use, and "5" with the number of news articles you want to display.

  4. Add the shortcode to your WordPress page or post.

Method 3: Using a Custom PHP Code

  1. Create a new PHP file in your WordPress theme directory (e.g., functions.php).

  2. Add the following code to the file:

    function google_news() {
    $query = 'your_query';
    $num = 5;
    $url = 'https://news.google.com/news/section?q='. urlencode($query). '&num='. $num;
    $html = file_get_contents($url);
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($html);
    libxml_clear_errors();
    $xpath = new DOMXPath($dom);
    $news_items = $xpath->query('//div[@class="g"]');
    $output = '';
    foreach ($news_items as $item) {
     $output.= '<h2>'. $item->getElementsByTagName('h2')->item(0)->nodeValue. '</h2>';
     $output.= '<p>'. $item->getElementsByTagName('p')->item(0)->nodeValue. '</p>';
    }
    return $output;
    }
    add_shortcode('google_news', 'google_news');

    Replace "your_query" with the search query you want to use, and "5" with the number of news articles you want to display.

  3. Add the shortcode [google_news] to your WordPress page or post.

Method 4: Using a Widget

  1. Install and activate a plugin like "Widget Logic" or "Widget Settings".

  2. Create a new widget by going to the plugin settings and adding a new widget.

  3. Use the following code to fetch Google News:

    <div>
    <h2>Google News</h2>
    <ul>
     <?php
     $query = 'your_query';
     $num = 5;
     $url = 'https://news.google.com/news/section?q='. urlencode($query). '&num='. $num;
     $html = file_get_contents($url);
     $dom = new DOMDocument();
     libxml_use_internal_errors(true);
     $dom->loadHTML($html);
     libxml_clear_errors();
     $xpath = new DOMXPath($dom);
     $news_items = $xpath->query('//div[@class="g"]');
     foreach ($news_items as $item) {
       echo '<li><h2>'. $item->getElementsByTagName('h2')->item(0)->nodeValue. '</h2><p>'. $item->getElementsByTagName('p')->item(0)->nodeValue. '</p></li>';
     }
    ?>
    </ul>
    </div>

    Replace "your_query" with the search query you want to use, and "5" with the number of news articles you want to display.

  4. Add the widget to your WordPress sidebar or footer.

Note: Make sure to replace "your_query" with the actual search query you want to use, and adjust the number of news articles to display as needed. Additionally, be aware of Google's terms of service and ensure that your use of Google News complies with their guidelines.