• Resolved Ted Slater

    (@tedslater)


    I’d like to save the value of {pid} to a variable, so I can do something if {pid} equals get_the_ID()

    Is it possible to save the values of the content tags into variables?

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

    (@hcabrera)

    Hi Ted!

    If you’re trying to format the HTML output based in post ID being the same as the current post/page ID, then you could hook into the not-yet-documented wpp_post_class filter hook to assign a specific class to the LI tag if the condition is met:

    function wp4654_wpp_post_class( $wpp_post_class, $post_id ){
    
        // We want to do this only while viewing a post / page / custom post type
        if ( is_single() ) {
    
            $current_post_id = get_queried_object_id(); // we need to do it this way as the widget is probably outside the loop so get_the_ID() might not work as expected
    
            if ( $current_post_id == $post_id ) {
                $wpp_post_class .= ' your-class-here';
            }
    
        }
    
        return $wpp_post_class;
    
    }
    add_filter( 'wpp_post_class', 'wp4654_wpp_post_class', 10, 2 );

    The caveat is that this only works if you’re using the stock HTML markup. If you’re using any of the built-in ways to customize the HTML output of the widget, then you’ll need to hook into wpp_custom_html or wpp_post and apply the logic from above (with a few additional changes, of course) to achieve the same effect.

    P. S.: I didn’t test this code but it should work as is.

    Thread Starter Ted Slater

    (@tedslater)

    Hm. I really appreciate the reply. Sadly, it’s just not working for me. :-/

    Plugin Author Hector Cabrera

    (@hcabrera)

    Show me some code and I might be able to help.

    Thread Starter Ted Slater

    (@tedslater)

    Basically, I want to show the top trending headline within the body of all posts … except on the top trending post, where I want to show the latest post headline instead.

    I’m editing the single.php file, and am working within the loop. I figured if I could pull in the ID for that top trending post, I could assign it to a variable. And then use a simple if/then statement to show X if top trending post ID equals current post ID, else show Y.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Ok, that helps a bit.

    Here’s how I would do it:

    // Somewhere inside the loop
    
    $args = array(
        'post_type' => 'post',
        'range' => 'last7days',
        'limit' => 1
    );
    $popular_posts = new WPP_Query( $args );
    
    // We have at least one popular post to show
    if ( !empty($popular_posts->get_posts()) ) {
    
        // The current post is the top post...
        if ( get_the_ID() == $popular_posts->get_posts()[0]->id ) {
            // ...so let's show the latest post headline here
        }
        // The current post isn't the top post...
        else {
            // ... so let's show the top post headline here
        }
    
    }

    I didn’t test this though so give it a shot and let me know how it goes.

    Thread Starter Ted Slater

    (@tedslater)

    That worked!

    If you’re interested in seeing it in action, you can go to hxxps://constitution.com/racial-amalgamation/ — you’ll see the “trending” headline as the 4th paragraph.

    Click that, and in the ensuing post the 4th paragraph will be the latest published post headline.

    Click that, and in the ensuing post the 4th paragraph will be the top trending headline.

    Works like a charm! I really appreciate your help, Héctor!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Don’t mention it. I’m glad I could help! ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘output content tags to variables/’ is closed to new replies.