• Resolved wpmhweb

    (@wppit)


    Hello,
    I need to be able to change {text_title} with variable to display a alternative headline instead of the main headline.
    I need to modify the code below to call a different title, is there any way I can do this?
    'post_html' => '<li><a> {thumb_img}<h2>{text_title}</h2></a></li>'

    I was trying something like this, but didn’t work:

    'post_html' => <li><a> {thumb_img} <h2>' . $second_title . '</h2> </a></li>'

    Thanks,

    https://www.ads-software.com/plugins/wordpress-popular-posts/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi there!

    Generally speaking there’s no way to add custom features (such as a custom post title) to plugin / widget shortcodes. Only those that the shortcode itself includes.

    WPP offers custom filter hooks you can use to modify its output which allows you to add your custom title and other changes as well.

    Thread Starter wpmhweb

    (@wppit)

    Amazing,
    I didn’t know about the hooks, it seems that it’s actually what I need.

    Thanks
    PS: Awesome work with WPP, keep up the great work.

    Thread Starter wpmhweb

    (@wppit)

    Hello,

    I’m not an expert, so it seems I can’t see to figure this out.
    I got the wpp hook working, but when try to add the alternative title function it doesn’t work.

    This is technically what I need:

    If post has alternative title,
    then show the alternative title,
    if not, then show the original title.

    This is the function I’m been trying to create

    function get_my_custom_title($post_id){
      
     // Get post data
        $the_post = get_post( $post_id );
        // Get post_content
        $the_title_custom = $the_post->the_title;
      
       if ($the_title_custom = get_post_meta($post->id, 'field_alt_title', true))  {
                  echo $the_title_custom;
                   } else {
                get_the_title();
       }
      $the_title_custom = $the_title_custom;
        
        return $the_title_custom;
    
    }

    Inside the function my_custom_popular_posts_html_list, this is what I’ve got:

    function my_custom_popular_posts_html_list( $mostpopular, $instance ){
        $output = '<ol class="wpp-list new">';
    
        // loop the array of popular posts objects
        foreach( $mostpopular as $popular ) {
    
           $excerpt = ''; // Excerpt placeholder
    
            // Excerpt option checked, build excerpt tag
            if ( $instance['post-excerpt']['active'] ) {
    
                $excerpt = get_excerpt_by_id( $popular->id );
                if ( !empty( $excerpt ) ) { 
                    $excerpt = '<div class="wpp-excerpt">' . $excerpt . '</div>';
                } 
    
            }
          
     
           $the_title_custom = get_my_custom_title($popular->id);
            if ( !empty( $the_title_custom ) ) { 
                    $the_title_custom = $the_title_custom;
                } 
          
          
            $output .= "<li>";
            $output .= "<a href=\"" . get_the_permalink( $popular->id ) . "\" title=\"" . esc_attr( $popular->title ) . "\"><h2 class=\"entry-title\">" .  esc_attr( $the_title_custom ) .  "</h2>";
            $output .="<div>" . get_the_post_thumbnail( $popular->id ) . "</div>";
            $output .= $excerpt;
            $output .= "</a></li>" . "\n";
    
        }
    
        $output .= '</ol>'; 
     
        return $output;
    }
    
    add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );

    Any help will be much appreciated!

    Thanks,

    Thread Starter wpmhweb

    (@wppit)

    Never mind,
    I think I got it,

    if ($the_title_custom = get_post_meta($popular->id, 'field_alt_title', true)) {
                $the_title_custom = $the_title_custom;
            } else {
                $the_title_custom = $popular->title;
            }
    • This reply was modified 8 years, 2 months ago by wpmhweb.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to display alternative post title’ is closed to new replies.