京都大学東南アジア地域研究研究所 情報処理室

List View for Posts

The plugin is the shortcode for comprehensively displaying the list view for pages, posts, custom posts, events for The Event Calendar plugin. The plugin supports the multi language with WPML plugin.

The HTML output templates except for the default template (“li”) continue to be adjusted.

List View for Posts

History

Shortcode

The list view on the posts are displayed. The HTML tag is <li> and <span>.

The HTML tag can change to  <p> and <span>.
4 templates (li, p, dd, lip) are prepared .
If you want to use the original template, please use the hook.

Shortcode options (Version 1.0)

You can use various settings, such as a category view, the view of a custom post type name, a view of a future post, the change of a date format, using the shortcode options. Then, you can specific the display area, such as from 50 item to 100 items.

If  WPML plugin is activated, the only current language of the  list view is displayed. If you want to specific the language, you can use  wpml_lang option. Then, please check  “Translate”  option on posts, custom post, page, taxonomy  in  WPML setting. In case of activating WPML plugin, the plugin displays only the data which is turned on “Translate” setting in WPML setting.

The description in detail is as below:

 

Case Example

Case 1) View of plural categories on a custom post type

The Shortcode is as below;

Output HTML code is as below;

<li class='list-view-posts_item'>
  <span class='list-view-posts_date'>29/08/2017</span>
  <span class='list-view-posts_post_type'>FAQ</span>
  <span class='list-view-posts_category_troubleshooting'>
     <a class='list-view-posts_category_link_troubleshooting'> href='***/faq_type/troubleshooting/'>Troubleshooting</a>
  </span>
  <span class='list-view-posts_category_windows'>
     <a class='list-view-posts_category_link_windows' href='http://localhost/wordpress-single/os/windows/'>Windows</a>
  </span> 
  <a class='list-view-posts_link' href='****/faq/*****'>How to rescue the data in Windows</a>
</li>

 

How to use the Hook

In the following description, the code is set on functions.php in the theme.
To prevent an illegal change,  the hook is enabled in case of matching “hook_secret_key” option’s value and “hook_secret_key” return value in the hook.

Arguments which can use on the Hook

The arguments are given as $out_atts .

Set extract($out_atts);  code, so you can use the following variables.
A part of variables were changed for easily coding.

Hook: lvp_each_item

Try to ouput the following HTML template (“lip” template  / includes/tags/lip.php).

<li class='list-view-posts_item'>
  <p class='list-view-posts_date'><span>29/08/2019</span></p>
  <p class='list-view-posts_post_type'><span>FAQ</span></p>
  <p class='list-view-posts_category_troubleshooting'>
     <span><a class='list-view-posts_category_link_troubleshooting' href='***/faq_type/troubleshooting/'>Troubleshooting</a></span>
  </p>
  <p class='list-view-posts_category_windows'>
     <span><a class='list-view-posts_category_link_windows' href='***/os/windows/'>Windows</a></span>
  </p> 
  <a class='list-view-posts_link' href='****/faq/*****'>How to rescue the data in Windows</a>
</li>

 

Firstly, set “hook_secret_key” option.
Actually, please set complex characters.

Then, add the following code to functions.php in the theme.
Please check   return array(‘hook_secret_key’=>’hogehoge‘, ‘data’=>$out_temp);  code.    ‘hook_secret_key’ value is same as”hook_secret_key” option.
If the values are not matched, the hook is ignored.
Then, the following code is a little complex because of the category processing. If you don’t need to display a category, the coding will be very short.

add_filter( 'lvp_each_item' , 'lvp_each_item_fix' , 10 , 2 );

function lvp_each_item_fix($out, $out_atts){
   extract($out_atts);

$output_category_temp = '';
$output_category_temp_post_type = '';
$output_category_temp_category = '';

if(!empty($enable_view_post_type)):
    if( ! isset($default_post_types[get_post_type_object(get_post_type())->name]) ? $default_post_types[get_post_type_object(get_post_type())->name] : "" ):
        $custom_category_label = esc_html( get_post_type_object(get_post_type())->label );
        $output_category_temp_post_type = "<p class='{$html_tag_class}_post_type'><span>$custom_category_label</span></p>";
    endif;
endif;

if(!empty($enable_view_category)):
    foreach($category_taxonomy as $cat):
        $terms = get_the_terms($post->ID, $cat);
        if($terms && !is_wp_error($terms)):
            $term  = esc_html( isset($terms[0]->name) ? $terms[0]->name : "" ); // Only get first value in the terms.
            $term_slug = esc_attr( isset($terms[0]->slug) ? $terms[0]->slug : "");
            if (strtolower($term) === 'uncategorized'): // 'uncategorized' is ignored.
                 continue;
            endif;
            $term_link = get_term_link($terms[0]);
            $output_category_temp_category .= "<p class='{$html_tag_class}_category_{$term_slug}'>";
            if(!is_wp_error($term_link) && !is_wp_error($term)):
                $output_category_temp_category .= "<span><a class='{$html_tag_class}_category_link_{$term_slug}' href='$term_link'>$term</a></span></p>";
            else:
                $output_category_temp_category .= "<span>$term</span></p>";
            endif;                              
        endif;
    endforeach;
endif;

$output_category_temp = $output_category_temp_post_type . $output_category_temp_category;

$out_temp = <<< ___EOF___
 <li class='{$html_tag_class}_item'><p class='{$html_tag_class}_date'>$date</p>$output_category_temp<a class='{$html_tag_class}_link' href='$link'>$title</a></li>
 
___EOF___;

    return array('hook_secret_key'=>'hogehoge', 'data'=>$out_temp);

}

 

If you change the output code each shortcode, use “id” option.

[list-view-posts ” hook_secret_key=”hogehoge” id=1]

[list-view-posts ” hook_secret_key=”hogehoge” id=”case2″]

 

Created on August 29, 2017 @kimipooh

Exit mobile version