• Resolved bogdandimitrov

    (@bogastyle)


    Hi,

    I’m trying to list all custom posts like unordered list. My code looks like:

    <ul class="slideshow">
    	<?php
    	$Q = new GetPostsQuery();
    	$args = array();
    	$args['post_type'] = 'slideshow';
    	$results = $Q->get_posts($args);
    
    	foreach ($results as $r) { ?>
    
    			<li class="slide">
    				<h1 class="slide_title">
    					<?php print $r['post_title']; ?>
    				</h1>
    				<div class="slide_content">
    					<?php print $r['post_content'];?>
    				</div>
    				<img src="<?php echo get_custom_field('slideshow_img_one:to_image_src'); ?>" />
    				<img src="<?php print_custom_field('slideshow_img_two:to_image_src'); ?>" />
    			</li>
    
    	<?php } ?>
    </ul>

    But can’t print custom fields – slideshow_img_one and slideshow_img_two.

    Can You help me, please.

    https://www.ads-software.com/plugins/custom-content-type-manager/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    You can’t use get_custom_field or print_custom_field when in a loop like that because the context of the variables: those functions are meant for the single-page templates only.

    When looping over values in a case like this, the task becomes converting the post id (i.e. the asset id) into its src or url. You can use WP’s built-in functions for this, e.g. https://codex.www.ads-software.com/Function_Reference/wp_get_attachment_image_src : just pass it the id of the post in question. See updated example at https://code.google.com/p/wordpress-custom-content-type-manager/wiki/to_image_src_OutputFilter

    Thread Starter bogdandimitrov

    (@bogastyle)

    I see. Thank You sir.
    Actually I made it with custom query like this.

    <?php
    	$loop = new WP_Query(array('post_type' => 'custom', 'posts_per_page' => -1));
    ?>
    <div id="homeSlides" class="carousel slide" data-wrap="false">
    	<div class="carousel-inner">
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    		<!-- Carousel items -->
    		<div class="item">
    	   	<img src="<?php print_custom_field('slideshow_img_one:to_image_src'); ?>" />
    		<img src="<?php print_custom_field('slideshow_img_two:to_image_src'); ?>" />
    
    	<div class="slide_content">
    	        <h1 class="slide_title">
    		<?php the_title(); ?>
    		</h1>
    									<?php the_content();?>
    	</div>
         </div>
    <?php endwhile; ?>
    </div>
    
    	<!-- Carousel nav -->
    	<a class="carousel-control left" href="#homeSlides" data-slide="prev">&lsaquo;</a>
    	<a class="carousel-control right" href="#homeSlides" data-slide="next">&rsaquo;</a>			
    
    </div>
    <?php wp_reset_query(); ?>
    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    My mind explodes and my heart dies every time WP_Query uses global variables like that, but if it works, great!

    Thread Starter bogdandimitrov

    (@bogastyle)

    ?? Don’t scare me man, please!

    I’m not developer and no developer friends around. So I fight alone.

    I’m open for advices from real developers and more mindful persons – always. That is reason to post questions in forums like this.

    ??
    Anyway You are one of this persons that always try to help other.

    Regards

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    If your solution works for the problem, that’s great: that’s the most important thing. The immediate problem was solved.

    WP isn’t really developer friendly in my opinion: it deserves massive recognition for bringing PHP to the masses, but the quality and structure of its code is sometimes painfully bad, and although I get flamed for saying things like that I think it’s good for users to know the limitations of the software they are using. In this case, the habit of WP of relying on global variables makes it difficult to see what’s going on with its code, and it can sometimes make the code less secure, and it usually makes the code less testable. So those can be real concerns in the longterm, but again, the immediate problem was solved, so you can take my reservations with a grain of salt.

    Thread Starter bogdandimitrov

    (@bogastyle)

    I see.
    Thank You again.
    Best Regards.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to print custom fields when using GeTPostQuery’ is closed to new replies.