• Resolved ElevenTwenty2

    (@eleventwenty2)


    I’m setting up a custom ‘Artists’ page template that will include a section that will display latest posts from a category. However, since that template is going to be used for multiple ‘Artists’, I want to be able to add a Custom Field for the category section so it knows to pull from that specific category.

    I’ve tried the following, but it doesn’t seem to work.

    <?php
    	$audiocategory = get_post_meta($post->ID, 'artist-audio-category', true);
    	$audio = new WP_Query('cat=$audiocategory');
      while ($audio->have_posts()) : $audio->the_post(); ?>

    Any help would be appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter ElevenTwenty2

    (@eleventwenty2)

    did you mean, https://codex.www.ads-software.com/Template_Tags/query_posts#Custom_Field_Parameters ?

    This method seems as if it’d take more work than it should. To name each post with a meta tag that I want to feed to this roll.

    Thread Starter ElevenTwenty2

    (@eleventwenty2)

    Hmm it seems if I replace the $post->ID with the Page ID # it seems to work fine. I use $post->ID on all previous get_post_meta functions on the same page, and they work fine. Would the WP_Query be causing the problem?

    Thread Starter ElevenTwenty2

    (@eleventwenty2)

    Anyone? Maybe I didn’t explain it too well, so I’m gonna try and reiterate what I’m trying to do.

    I want to have one universal template for Artists.

    So whenever the client adds a new Artist Page, he’ll select the “Artist Template” and he’ll use Custom Fields to enter all the details needed so that it displays that specific artists information.

    For example:

    https://www.soundrealmstudios.com/artists/drake/
    https://www.soundrealmstudios.com/artists/sdot/

    Each page uses the same template, however I could only get the post meta to work on everything but a WP loop. As you can see in the Top Tracks and Audio/Video section it pulls all the posts and not the specific category I’m telling it to use (through Custom Fields).

    I’m still a little confused. You are using custom fields to display the Top Tracks that are entered into the field? It sounds like you’re actually typing a category number into a custom field, is this the case?

    If this is a single.php page everything should work just fine, but you are wanting to display single information on a page.php file? Let me know.

    https://www.doc4design.com

    Thread Starter ElevenTwenty2

    (@eleventwenty2)

    Yes, I’m entering a category number into the custom field value.

    the page is a custom page I created artist-template.php using the instructions from https://codex.www.ads-software.com/Pages#Creating_Your_Own_Page_Templates

    here’s the exact code i’m using in one of the sections.

    <!-- begin top tracks -->
    			<div id="artist-toptracks">
    				<h2></h2>
    				<div class="tptracksbox">
    
    					<?php
    						$toptracks = get_post_meta($post->ID, 'artist-audio-category', true);
    						$tracks = new WP_Query('cat=' . $toptracks . '&numberposts=5');
      						while ($tracks->have_posts()) : $tracks->the_post();
    					?>
    
    					<!-- begin post roll-->
    					<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    					<div class="instru-content2">
    						<?php the_content(); ?>
    					</div>
    
    					<?php endwhile; ?>
    				</div>
    			</div>
    			<!-- end top tracks -->

    Could it be that it’s trying to pull the custom field from the posts instead of the page?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Retrieve Category ID from Custom Field For WP Loop’ is closed to new replies.