• Resolved Ralf Koller

    (@rkoller)


    Hi i ran into an odd problem. I am running WordPress 3.8 and the relevant plugins are advanced custom fields (including the repeater field add on) and custom post types ui. I’ve created a custom field for projects and display them in a custom post type.

    The custom field consists of:

    1) title
    2) Project summary picture (fieldname: pro_preview, field type: image)
    3)Project description (fieldname: pro_description, field type: text area)
    4) project services (fieldname: pro_services field type: repeater)
    4.1) service (fieldnamen: pro_service_single field type: text)
    5.) project images (fieldname: pro_images, field type: repeater)
    5.1.) image (fieldname: pro_impressions, field type: image)

    I have an overview page (template-projects.php) which lists all projects properly. If i mouse over one of the projects (e.g. running) the href shows localhost:8888/testsite/projects/running, or localhost:8888/testsite/projects/bike if i mouse over e.g. the bike project. but no matter which project i choose each time only one certain single other project is shown. no running no bike just lets name it nutrition. but i have no idea why. my single page is called single-projects.php . and also the adressbar is showing the correct adress like e.g. localhost:8888/testsite/projects/running in the case i’ve clicked running – but the content shows nutrition.

    The query part on the single-projects.php page looks like that. For testing purpose i’ve only queried the title, description and services.

    <?php
    		$args = array(
    			'post_type' => 'projects',
    			'posts_per_page' => 1
    		);
    		$the_query = new WP_Query( $args );
    	?>
    	<? if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<h3><?php the_title(); ?></h3>
    		<p><?php the_field( 'pro_description' ); ?></p>
    		<h4>Our services</h4>
    		<?php if(get_field( 'pro_services' ) ): ?>
    				<ul>
    				<?php while( has_sub_field( 'pro_services' ) ): ?>
    					<li><?php the_sub_field( 'pro_service_single' ); ?></li>
    				<?php endwhile; ?>
    				</ul>
    		<?php endif; ?>
    	<?php endwhile; ?>
    		<?php wp_reset_postdata(); ?>
    	<?php else: ?>
    		<p>Something is gone wrong...</p>
    	<?php endif; ?>

    I am out of ideas what is going wrong here. :/ any hints are appreciated. best regards ralf

Viewing 2 replies - 1 through 2 (of 2 total)
  • C W (VYSO)

    (@cyril-washbrook)

    Every time, you are seeing the most recent post from the projects post type. This is because you have created a new query with WP_Query which instructs WordPress to do exactly this.

    What you actually want to do is simply rely on the main query, along the lines of:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- Stuff -->
    <?php endwhile; endif; ?>

    When the file name is single-[name of the post type].php, WordPress will do the rest for you.

    Thread Starter Ralf Koller

    (@rkoller)

    Coooool fixed the issue. But one thing drove me nuts, is it possible that WordPress has its issues with commented out lines of code?

    commented out the first part

    <!--
    			<?php
    			$args = array(
    				'post_type' => 'projects',
    				'posts_per_page' => 1
    			);
    			$the_query = new WP_Query( $args );
    		?>
    		<? if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    -->

    and replaced it with:

    <? if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    and i got “PHP Parse error: syntax error, unexpected end of file” always on the end of my template file on the get_footer call. if i deleted the commented out parts it worked. strange. but thanks for the explanation about the loop thing – understand it now . thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Always the same post of a custom post type is shown on a single page’ is closed to new replies.