詳細検索

WordPress shortcodes that show "NEW" if they contain the latest posts in each category

Avatar
by maeno
3 min read

WordPress shortcodes that show "NEW" if they contain the latest posts in each category
Translated from 日本語 • View original

[caption id="attachment_364" align="alignleft" width="320"]

特定カテゴリーに新着投稿があればNEWを表示するショートコード[/caption]

When we rebuilt our intranet with WordPress, one of the requests was to display the "NEW" mark in the category list of the menu that is placed as a sidebar if it contains the latest posts for each category.
So, I created a shortcode that specifies a specific category and displays the "NEW" mark next to the category name if the posts in that category contain the most recent ones, so I'll share it here. As an extension, you can also count the number of recent posts in a category and display the number of them (you can also make it look like the latest unread icon displayed in a FaceBook menu, etc.).

I searched for various shortcodes, but there are many shortcodes that display "NEW" next to the list of articles in WordPress, but all of them are the latest judgments for the article list, and I couldn't find any shortcodes for the latest judgments for categories, so I decided to make my own.
I used Kunitoshi Hoshino's WordPress community as a reference, "4 ways to display New! in WordPress by pattern"The article. His website introduced TIPS for the requests and needs unique to WordPress corporate sites, which was very helpful.

[php highlight="2-37,38"] function addnew($attr, $content){ extract(shortcode_atts(array( 'slug' => '', 'ttype' => 'pub', 'term' => 7, 'vtype' => 'str', ), $atts)); $today = date_i18n( 'U'); $ttype = ($ttype!='pub') ? 'mod' : 'pub'; $term = ($term!=7) ? intval($term) : 7; $vtype = ($vtype!='str') ? 'num' : 'str'; $value = ($content!='') ? do_shortcode($content) : 'new'; global $post; $cats = get_category_by_slug($slug); if(!$cats){ return ''; } else{ $cat = $cats->term_id; if(!$cat) return ''; } $last_post_ids = array(); $lastposts = get_posts('category='.$cat); foreach($lastposts as $lastpost) : $entry = ($ttype == 'pub') ? date_i18n('U', strtotime($lastpost->post_date)) : date_i18n('U', strtotime($lastpost->post_modified)); $interval = date('U', ($today - $entry)) / (60 * 60 * 24); if(intval($term) > $interval){ $last_post_ids[] = $lastpost->ID; } endforeach; if(count($last_post_ids) > 0){ if($vtype == 'str'){ $retstr = $value; } else{ $retstr = count($last_post_ids); } } wp_reset_query(); if($retstr != '') return $retstr; } add_shortcode('addnew', 'addnew'); [/php]

If you want to import it into the "Shortcode Exec PHP" plugin, you can copy lines 2~38 of the above source code or use this XML file(*Since it will be imported under the name "addnew", if there is already a shortcode with the same name, it will be overwritten.) Please note).
An example of how to use a shortcode in action is shown below.

  1. Display "new" if the specified category contains posts within 7 days of the publication date.

[code lang="html" light="true"]

  • Category Name[addnew slug="category slug name"]
  • [/code] 2. If the specified category contains posts within 7 days of the publication date, write "NEW!" in bold letters. Show

    [code lang="html" light="true"]

  • Category name [addnew slug="category slug name"]NEW![/addnew]
  • [/code] 3. Show the number of posts in the specified category that were last updated within 3 days.

    [code lang="html" light="true"]

  • Category name [addnew slug="category slug name" ttype="mod" term="3" vtype="num"][/addnew]
  • [/code] 4. Show category links if the specified category contains posts within 5 days of the publication date

    [code lang="html" light="true"] [addnew slug="slug name of category" ttype="pub" term="5" vtype="str"]

  • Category Name
  • [/addnew] [/code] 5. If the specified category contains posts that were last updated within 7 days, insert a SPAN tag to display the number icon before the link.

    [code lang="html" light="true"] [addnew slug="category slug name" ttype="mod" vtype="str"] [addnew slug="category slug name" vtype="num"] [/addnew] category name [/code]

    You can nest shortcodes, so I think it can be used in various cases depending on how you use it.

    <

    p style="text-indent:1em;"> If you want to use a shortcode in a widget such as a sidebar, please add the following code to the function.php of the theme you are using.
    [php light="true"] add_filter('widget_text', 'do_shortcode'); [/php]

    Related Articles