Skip Navigation

[Resolved] How to create a view of media images by category

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#1136179
Screen Shot 2018-10-29 at 2.06.06 PM.png

This seems like it should be simple, but I'm struggling.

I have created a new taxonomy, "Media Categories" and assigned it to the content type, "Media".

I created a view selecting the content type "Media" and with a query filter using the aforementioned media category (see attached screenshot).

The view is outputting some automatic html that I seem to have no control over and is not in my content template (I've removed the URLs here for brevity):

<p class="attachment"></p>

Again, the above markup is not in my content template or view html.

Is there a way to prevent that markup from showing, and to select which attributes to output? I want to generate something similar to the paragraph tag above, but on my own terms.

#1136655

Hi Shawn,

Thank you for contacting us and I'll be happy to assist.

Since the "Media" is a WordPress' native custom post type, it's title and body fields get affected by WordPress' own and the active theme's filter to handle media attachments.

To avoid the extra HTML that comes with the title shortcode, you can include output="raw" attribute to it:


[wpv-post-title output="raw"]

To get the actual file's URL in your media posts view, you can create a custom shortcode that makes use of "wp_get_attachment_url" function.
( ref: https://developer.wordpress.org/reference/functions/wp_get_attachment_url/ )

For example, you'll add the following code in your active theme's "functions.php" file, to register a shortcode [get-attachment-url]:


add_shortcode('get-attachment-url', 'get_attachment_url_fn');
function get_attachment_url_fn( $atts ) {

	$a = shortcode_atts( array(
		'id' => ''
	), $atts );

	if (!empty($a['id'])) {
		return wp_get_attachment_url( $a['id'] );
	}
	else
	{
		return "No id provided";
	}
	
}

In your view's content template, you can pass on the id of your current media post type like this:


[get-attachment-url id="[wpv-post-id]"]

I hope this helps.

regards,
Waqar

#1136953

I ended up in a very similar place to your suggestion. This is the shortcode I created; this way I can control the size returned. Hopefully this will help someone else.

add_shortcode( 'get_att_src', 'syss_get_attachment_src' );
function syss_get_attachment_src( $atts ) {
	extract( shortcode_atts( array(
		'att_id' => '',
		'att_size' => 'thumbnail',
		), $atts )
	);
	$image_attributes = wp_get_attachment_image_src( $att_id, $att_size, false );
	if ( $image_attributes ) {
		return $image_attributes[0];
	} else {
		return false;
	}
}

I put it into a custom plugin so that it's independent of the theme.

This is how I'm using in the loop editor:

<figure class="gallery-item">
      <a href="[get_att_src att_id='[wpv-post-id]' att_size='full']" title="click for a large version"><img src="[get_att_src att_id='[wpv-post-id]' att_size='thumbnail']" /></a>
 </figure>

It would be nice to be able to do this natively with Toolset, though, so it doesn't require custom code. Is it something you could add?

Thanks