0

I have used excerpt_more filter in a theme's functions.php and in a plugin's functions.php. Here is the code for the theme

  function theme_excerpt_more( $more ) {
     return '...';
  }
  add_filter('excerpt_more', 'theme_excerpt_more');

and here is the code for the plugin

   function newsbox_excerpt_more($more) {
   global $post;
   $options = get_option('newsbox_plugin_display_options');
   if(isset($options['read_more_text'])){
      $read_more_text = $options['read_more_text'];
   }
   else{
     $read_more_text = "Read More »";
   }
   return '...<a href="'. get_permalink($post->ID) . '">' . $read_more_text . '</a>';
 }
  add_filter('excerpt_more', 'newsbox_excerpt_more');

But only the theme's excerpt_more is working. I want the plugin excerpt_more to work on the post that will set in the shortcode. This is the shortcode

[news_box posts_per_page=-1 news_box_title="Latest News" post_type="latest_news" category ="Uncategorized"]

If I don't set post_type, then the plugin's excerpt_more will work on the default post.

Any suggestions to achieve this?

Edit: Full code

 function newsbox_new_loading_shortcod($atts){
 extract(shortcode_atts(array(
    'posts_per_page'=>'-1',
    'news_box_title'=>'Latest News',
    'post_type'=>'post',
    'category'=>''
),$atts,'news_box'));

$q = new WP_Query(
 array('posts_per_page' => $posts_per_page,'post_type'=>array($post_type),'category_name'=>$category));
$list = '<div class="panel panel-default">
                <div class="panel-heading"> <span class="glyphicon glyphicon-list-alt"></span><b>'.$news_box_title.'</b></div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-xs-12">
                            <ul class="news_list">';

                                while($q->have_posts()) : $q->the_post();

                                    $newsbox_post_img_src = wp_get_attachment_image_src( get_post_thumbnail_id(), '', false );

                                    $list .= '<li class="news-item">
                                     <table cellpadding="4">
                                        <tr>
                                           <td><img src="'.$newsbox_post_img_src[0].'" width="100" class="img-circle" /></td>
                                           <td>'.get_the_excerpt().'</td>
                                        </tr>
                                     </table>
                                  </li>';

                                endwhile;

                                wp_reset_postdata();

                            return $list . '</ul>
                        </div>
                    </div>
                </div>
                <div class="panel-footer"> </div>
            </div>';                            

 }
 add_shortcode('news_box','newsbox_new_loading_shortcod');
  • Why you need 2 functions, can't this be done with a single one? Also you have $post object, so you can check with $post->post_type. – Chittaranjan Sep 2 '14 at 8:13
  • I am developing a plugin not only for me. It is for wordpress community. Now if a user use excerpt_more to there plugin and also use my plugin then it will make a conflict. For that reason i need to check that. @Chittaranjan – Alamin Sep 2 '14 at 8:26
  • Please post complete shortcode code – Pieter Goosen Sep 2 '14 at 11:28
  • Try higher priority value in the filter like add_filter('excerpt_more', 'newsbox_excerpt_more', 100); – Nilambar Sharma Sep 2 '14 at 11:32
  • You can check here complete shortcode @PieterGoosen wordpress.stackexchange.com/questions/159818/… – Alamin Sep 2 '14 at 11:35
0

As said in my edit to my answer to your previous post, I have rewritten your complete shortcode to be a bit more useful, and I also removed the extract() function which is a bad function to use.

To answer this question, I had to further modify your shortcode. To avoid clash with other excerpt functions, I have removed the_excerpt() and have replaced it with wp_trim_words. This will also void the need for the excerpt more function, so you can delete that. All that functionality is now build into the shortcode

Here is the update code. Just to renote: Needs PHP 5.4+

add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        [
            'posts_per_page'    => '-1',
            'news_box_title'    => 'Latest News',
            'news_box_more'     => '',
            'post_type'         => 'post',
            'taxonomy'          => '',
            'terms'             => '',
            'category'          => '',
        ], 
        $atts 
    );

    if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
      if( '' == $a['category'] ) {

        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
        ];


      }else{
        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'category_name'     => $a['category'],

        ];
     }

    }else{
        $args = [
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'tax_query'         => [
                [
                    'taxonomy'  => $a['taxonomy'],
                    'field'     => 'slug',
                    'terms'     => $a['terms'],
                ]
            ]
        ];
    }

    //The following lines is for the excerpt more text NEW!!
    if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
        $read_more_text = $a['news_box_more'];
    }else {
        $read_more_text = "Read More &raquo;";
    }
    // end of excerpt more text code

    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 

        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' ); 

            // wp_trim_words function NEW!!
            $content = get_the_content();
            $trimmed_content = wp_trim_words( $content, 55, '<a href="'. get_permalink() .'"> ...' . $read_more_text . '</a>' ); 
            // wp_trim_words function
            ?>


            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php echo $trimmed_content; // Replaced the_excerpt() ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}

For support for PHP < 5.4, you can do the following for the shortcode function.

add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        array(
            'posts_per_page'    => '-1',
            'news_box_title'    => 'Latest News',
            'news_box_more'     => '',
            'post_type'         => 'post',
            'taxonomy'          => '',
            'terms'             => '',
            'category'          => '',
        ), 
        $atts 
    );

    if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
      if( '' == $a['category'] ) {

        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
        );


      }else{
        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'category_name'     => $a['category'],

        );
     }

    }else{
        $args = array(
            'posts_per_page'    => $a['posts_per_page'],
            'post_type'         => $a['post_type'],
            'tax_query'         => array(
                array(
                    'taxonomy'  => $a['taxonomy'],
                    'field'     => 'slug',
                    'terms'     => $a['terms'],
                ),
            ),
        );
    }

    //The following lines is for the excerpt more text NEW!!
    if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
        $read_more_text = $a['news_box_more'];
    }else {
        $read_more_text = "Read More &raquo;";
    }
    // end of excerpt more text code

    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 

        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' ); 

            // wp_trim_words function NEW!!
            $content = get_the_content();
            $trimmed_content = wp_trim_words( $content, 55, '<a href="'. get_permalink() .'"> ...' . $read_more_text . '</a>' ); 
            // wp_trim_words function
            ?>


            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php echo $trimmed_content; // Replaced the_excerpt() ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}

Just for interest sake, you have the following in your shortcode 'news_box_title' => Latest News',, but I can see where it fits in. I have also used it in my code, but have not used it anywhere. You will have to place it where you need it.

| improve this answer | |
  • Comments are not for extended discussion; this conversation has been moved to chat. – kaiser Sep 3 '14 at 15:26
0

Try this:

function newsbox_excerpt_more($more) {
    global $post;
    if ( ! ( 'post' == $post->post_type ) ) {
        return $more;
    }
    $options = get_option('newsbox_plugin_display_options');
    if(isset($options['read_more_text'])){
        $read_more_text = $options['read_more_text'];
    }
    else {
        $read_more_text = "Read More &raquo;";
    }
    return '...<a href="'. get_permalink($post->ID) . '">' . $read_more_text . '</a>';
}
add_filter('excerpt_more', 'newsbox_excerpt_more', 100);

Here filter priority is set to 100. Also inside the function post_type is checked. If not post, then it will return default. Otherwise it will return as you write in the function.

| improve this answer | |
  • No luck. @Nilambar – Alamin Sep 2 '14 at 12:13
  • Did you change post to latest_news in post type comparison? – Nilambar Sharma Sep 2 '14 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.