Support » Developing with WordPress » Template Tag for Media Categories

  • Is there a template tag similar to

    <?php is_category(); ?>

    but it outputs the categories of a site’s media categories instead of the blog posts categories? I’ve searched the forums, google, and have tried all sorts of variations of the above with “media” added and nothing seems to be working.

    I’m trying to customize the Attachment page template for images on my site and I’d like to incorporate a few conditional statements for certain media categories to display different image descriptions on the Attachment pages.

    The link attached to this question is an example of my Attachment page.

    Thanks
    Stephen

    • This topic was modified 3 months, 2 weeks ago by  sgclark.
    • This topic was modified 3 months, 2 weeks ago by  sgclark.
    • This topic was modified 3 months, 2 weeks ago by  sgclark.

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • By default, the taxonomy “categories” isn’t tied to Media. If you are trying to check for a specific taxonomy you would use is_tax()

    So for Media, would the php code be this?

    <?php is_tax('media'); ?>

    How would I write out the ‘if / elseif’ code to essentially enable the following on the example Attachment page linked in my post:

    <?php If ?> the Media/Image is in ‘this’ media category, display ‘this’ description under the image, <? elseif ?> ‘that’ media category, display ‘that’ description under the image.

    • This reply was modified 3 months, 2 weeks ago by  sgclark.
    Moderator bcworkz

    (@bcworkz)

    Any of the is_whatever() functions do not output anything. They return boolean values you can use to construct conditional control structures. is_tax() will accept an additional argument of a term slug to determine if the query is for a particular term.

    is_tax('media') will return true if the requested page is a listing of posts assigned terms in the ‘media’ taxonomy.

    is_tax('media', 'music') will return true if the requested page is a listing of posts assigned the term ‘music’ of the ‘media’ taxonomy.

    Instead of building some sizable conditional structure that attempts to match query terms, it may make more sense to simply output the requested taxonomy and term names, whatever they may be. For example, the_archive_title( '<h1 class="page-title">', '</h1>' );
    might output:
    <h1 class="page-title">Media: Music</h1>

    Thanks @bcworkz for responding. What you outlined helps but it is not solving for what I’m trying to do.

    I have a lot of Desktop Wallpapers on my site (see here--> www.sgclark.com/wallpapers/ncaa for example)

    I find that people somehow enter my site via Google Image Search and land on the ‘image attachment’ page similar to the example below.
    (See example Attachment Page in original post and here --> https://www.sgclark.com/sandbox/wallpapers/college/ncaa_florida_state_seminoles_helmet/)
    What I was hoping to do was for the paragraph of text under the image (in the FSU example above, the paragraph starts with: “This is a desktop/device wallpaper…”), I wanted to do a simple php ‘if / elseif’ based on the media category (i.e. ‘wallpapers’ or in this case, ‘ncaa’) where all my wallpaper images would get something similar to what is currently under the example above, but all other images on my site that have attachment pages, they would get a more generic paragraph. I was assuming that I could key that logic off of the media category, either “for this media category, display ‘this’ paragraph, but for all other media categories, display ‘that’ paragraph”

    Hope this makes sense.
    Thanks
    Stephen

    Moderator bcworkz

    (@bcworkz)

    I think I get it. I had imagined you needed a more fine grained control of what happens with multiple taxonomy term listings. When it comes to specific attachment pages, the is_tax() function will not meet your needs because it only works for taxonomy term archive requests like example.com/media/ncaa/ which lists all posts assigned the “ncaa” term. Singular attachment pages are another matter entirely. Your conditional code needs to get the term assigned to the current post and check if it is “ncaa” or not. Something like this (must occur within “The Loop”):

    <?php if ('ncaa' == wp_get_post_terms( get_the_ID(), 'media', ['fields'=>'names',])[0]): ?>
    This paragraph is used for posts assigned only
    the term "ncaa" in taxonomy "media".
    <?php else: ?>
    This paragraph is used for posts NOT assigned
    the term "ncaa" in taxonomy "media".
    <?php endif; ?>

    This assumes only one “media” term is assigned to any NCAA image attachment. If there could be multiple terms assigned, we need a more elaborate check for the term.

    Thanks for your response! That looks like what I’m trying to do. I’ll give that a shot and let you know if it works.

    I can tell you that I never *ever* would have figured out how to draft that code by myself, so THANK YOU!

    Stephen

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. That code is untested BTW, if you have trouble, let me know. For one thing, older PHP versions will have trouble with the [] array syntax.

    Don’t sell yourself short, I’ll wager you could have eventually put something together, given the right direction. But going with is_category() would have never gotten you there 🙂

    I tried the code as you wrote it on the page and it didn’t work. I messed around with it a bit more, and tried a few options but it did not work. I’ll try it again later in the week. Don’t have the time to dig into this now. The site has the most updated version of PHP (7.x) via my host.

    Your ‘don’t sell yourself short’ comment above made me think of Caddyshack. I am a tremendous slouch. LOL 😀

    Moderator bcworkz

    (@bcworkz)

    Lulz, I’d forgotten that line, there were so many good ones in that movie!

    I tested my code on my page template. Altering the particulars to fit my taxonomy and terms for pages of course. It works fine for me. Possible reasons it doesn’t work for you: Code outside of loop. More than one term assigned. Mismatch between term label and name, so failing to match “ncaa”. Even the name “NCAA” may not match on some systems which are case sensitive.

    It could be I’m trying to get terms from the wrong post. Such as if ncaa_nevada-las_vegas_runnin_rebels_new is a post which contains an attachment, or it is an attachment but the term is assigned to the parent post.

    I think it may be the ‘more than one term assigned to it’ issue. I generally classify them into a parent and child category (i.e for UNLV, it would be ‘NCAA’ and their conference ‘Mountain West (slug:mtnwest). Thought this would be a simple if/elseif statement. Oh well. Thanks for your help. I’ll play around with it more.

    Moderator bcworkz

    (@bcworkz)

    In that case the conditional statement should be more like
    <?php if ( in_array('NCAA', wp_get_post_terms( get_the_ID(), 'media', ['fields'=>'names',]))): ?>

    I misspoke earlier about labels and names. The names returned by wp_get_post_terms() ARE the labels seen on templates and not the slugs normally used by code.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.