Support » Fixing WordPress » Custom Post Types and Custom Fields not showing up

  • Resolved sebgraus

    (@sebgraus)


    Hi,

    I’m trying to implement custom fields and post types in my page by using the Advanced Custom Post types plugin and the Custom Fields UI plugin. I’ve filled them out correctly but the php code won’t appear in my uploaded page. Here’s the code:

    <?php 
    
    /* Template Name: Bar */
    
     ?>
    
    <?php get_header(); ?>
    <p>This is the bar.php file</p>
    
    <?php
    
    	$sargs = array(
    		'post_type' => 'sidor'
    
    		);
    
    	$the_query = new WP_Query( $args );
    
    	?>
    
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<h1><?php the_title(); ?></h1>
     	  	<?php the_field( 'sidor' ); ?>
    
    	<?php endwhile; else: ?>
    		<p><?php _e('Ledsen, sidan du försöker hitta finns inte.'); ?></p>
    	<?php endif; ?>
    </div>
    
    <?php get_footer(); ?>

    As you see if you visit the page here http://marielaveau.se/wordpress/bar/ only the text “This is the bar.php file” shows up. I’m a bit confused about the ‘post_type’ => ‘sidor’ tag. What am I to fill in where I’ve now put ‘sidor’?

    Thanks!

Viewing 15 replies - 1 through 15 (of 15 total)
  • <p>This is the bar.php file</p>

    Will render as text on your page.

    You mention you’ve created a custom post type and a custom field.

    What is your custom post type named en what is your custom field named?

    Yes, but shouldn’t the php code show in the source-code file? When I press “view source file” in google crome the php code isn’t showing.

    My Custom Post type has the name “sidor”
    My custom Field group is called “sida”
    And I have multiple field names, one of them is “titel”, one of them “beskrivning”

    Thank’s for the help Mike!

    Personally this is a basic example of what I tend to use:

    $your_query = new WP_Query( $args );
    if ( $your_query -> have_posts() ) {
    	while ( $your_query -> have_posts() ) {
    ...
    	}
    }
    
    wp_reset_query();

    Isn’t that basically the same code that I’m using? I really don’t understand why it’s not working.

    Yes, but shouldn’t the php code show in the source-code file? When I press “view source file” in google crome the php code isn’t showing.

    No, your plugins will not add code to your php, you have to do this by hand.
    PHP code should never show up in view source. The output should, but apparently your code does not have output.

    so your code should look something like:

    <?php $args = array(
    	'post_type' => 'sidor',
    );
    
    $your_query = new WP_Query( $args );
    if ( $your_query -> have_posts() ) {
    	while ( $your_query -> have_posts() ) {
    ?>
    <h1><?php the_title(); ?></h1>
    <?php the_field( 'sida' ); ?>
    
    <?php } // end while
    else {
    ?>
    <p><?php _e('Ledsen, sidan du försöker hitta finns inte.'); ?></p>
    <?php } // end if ?>

    Typing this on an ipad, so please check the code carefully.

    If i’m not misstaken you’re using have_posts() without specifying which query should have the posts.

    Thanks Mike, however I’m not really sure what to fill in for $your_query tough? Sorry, this is my first attempt on wordpress-coding for a while.

    $your_query is just the name of the variable your’e loading the query into. You named it $the_query.

    So this shouldn’t matter as long as my code looks like you wrote it?

    <?php 
    
    /* Template Name: Bar */
    
     ?>
    
    <?php get_header(); ?>
    <p>This is the bar.php file</p>
    
    <?php $args = array(
    	'post_type' => 'sidor',
    );
    
    $your_query = new WP_Query( $args );
    if ( $your_query -> have_posts() ) {
    	while ( $your_query -> have_posts() ) {
    ?>
    <?php the_field( 'sida' ); ?>
    
    <?php } // end while else {
    ?>
    
    <p><?php _e('Ledsen, sidan du försöker hitta finns inte.'); ?></p>
    <?php } // end if ?>
    
    <?php get_footer(); ?>

    I’ve tried this out but sadly nothing happens.

    Could you link to the advanced custom post types plugin? I want to recreate your case but can’t find the plugin

    Ok, tried this and it works on my install:

    I created a custom post type with the plugin. Called it ‘sidor’.

    <?php
    $your_query = new WP_Query( array( 'post_type' => 'sidor', 'posts_per_page' => 100 ) );
    while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
    
    <h3><?php the_title() ?></h3>
    
    <?php endwhile; ?>

    Mike, you’re awesome! Thank you very much. Works for me aswell.

    Great! Good luck on your site!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Custom Post Types and Custom Fields not showing up’ is closed to new replies.