• Resolved gjyshi

    (@gjyshi)


    The plugin looks nice I integrated toa new theme that I’m developing the content etc is okay I’m done but I have a question:

    I’m trying to add a corner ribbon over the post thumbnail in archives and homepage but I would like to separate it via membership names by dedecting automatically, so I found a function in includes/metaboxes and edited to

    function pmpro_corner_ribbon( $term ) {
      global $post, $wpdb;
    	$membership_levels = pmpro_getAllLevels( true, true );
    	$membership_levels = pmpro_sort_levels_by_order( $membership_levels );
    	$page_levels = $wpdb->get_col( "SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '" . intval( $post->ID ) . "'" );
      foreach( $membership_levels as $level ) {
    
        echo '<div class="ribbon-wrapper"><div class="ribbon level=' . intval( $level->id ) . '">' . esc_html( $level->name ) . '</div></div>';
    
      }
    }
    add_action('pmpro_corner_ribbon','pmpro_corner_ribbon');

    The output is:

    <?php if(has_action('pmpro_corner_ribbon')) { do_action('pmpro_corner_ribbon'); } ?>

    it shows the ribbon for all posts including posts without membership is there any way to make it working?

Viewing 1 replies (of 1 total)
  • Thread Starter gjyshi

    (@gjyshi)

    I figured it out:

    function pmpro_corner_ribbon( $term ) {
      global $post, $wpdb;
    
    	//get queried object in case we check against that
    	if(!is_admin())
    		$queried_object = get_queried_object();
    	else
    		$queried_object = NULL;
    
    	//use post global or queried object if no $post_id was passed in
    	if(!$post_id && !empty($post) && !empty($post->ID))
    		$post_id = $post->ID;
    	elseif(!$post_id && !empty($queried_object) && !empty($queried_object->ID))
    		$post_id = $queried_object->ID;
    
    	//no post, return true (changed from false in version 1.7.2)
    	if(!$post_id)
    		return true;
    
    	//if no post or current_user object, set them up
    	if(isset($queried_object->ID) && !empty($queried_object->ID) && $post_id == $queried_object->ID)
    		$mypost = $queried_object;
    	elseif(isset($post->ID) && !empty($post->ID) && $post_id == $post->ID)
    		$mypost = $post;
    	else
    		$mypost = get_post($post_id);
    
    	//for these post types, we want to check the parent
    	if(isset($mypost->post_type) && in_array( $mypost->post_type, array("attachment", "revision")))
    	{
    		$mypost = get_post($mypost->post_parent);
    	}
    
    	// Allow plugins and themes to find the protected post
        $mypost = apply_filters( 'pmpro_membership_access_post', $mypost, $myuser );
    
    	if(isset($mypost->post_type) && $mypost->post_type == "post")
    	{
    		$post_categories = wp_get_post_categories($mypost->ID);
    
    		if(!$post_categories)
    		{
    			//just check for entries in the memberships_pages table
    			$sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'";
    		}
    		else
    		{
    			//are any of the post categories associated with membership levels? also check the memberships_pages table
    			$sqlQuery = "(SELECT m.id, m.name FROM $wpdb->pmpro_memberships_categories mc LEFT JOIN $wpdb->pmpro_membership_levels m ON mc.membership_id = m.id WHERE mc.category_id IN(" . implode(",", $post_categories) . ") AND m.id IS NOT NULL) UNION (SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "')";
    		}
    	}
    	else
    	{
    		//are any membership levels associated with this page?
    		$sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'";
    	}
      $post_membership_levels = $wpdb->get_results($sqlQuery);
    foreach( $post_membership_levels as $level ) {
    
        echo '<div class="ribbon-wrapper"><div class="ribbon level=' . intval( $level->id ) . '">' . esc_html( $level->name ) . '</div></div>';
    
      }
    }
    • This reply was modified 1 year, 10 months ago by gjyshi.
Viewing 1 replies (of 1 total)
  • The topic ‘Adding a ribbon with the membership name’ is closed to new replies.