WordPress.org

Forums

[resolved] Custom Post Types and Custom Fields not showing up (16 posts)

  1. sebgraus
    Member
    Posted 1 year ago #

    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!

  2. Mike
    Member
    Posted 1 year ago #

    <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?

  3. sebgraus
    Member
    Posted 1 year ago #

    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!

  4. Mike
    Member
    Posted 1 year ago #

    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();
  5. sebgraus
    Member
    Posted 1 year ago #

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

  6. Mike
    Member
    Posted 1 year ago #

    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.

  7. Mike
    Member
    Posted 1 year ago #

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

  8. Mike
    Member
    Posted 1 year ago #

  9. sebgraus
    Member
    Posted 1 year ago #

    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.

  10. Mike
    Member
    Posted 1 year ago #

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

  11. sebgraus
    Member
    Posted 1 year ago #

    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.

  12. Mike
    Member
    Posted 1 year ago #

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

  13. sebgraus
    Member
    Posted 1 year ago #

  14. Mike
    Member
    Posted 1 year ago #

    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; ?>
  15. sebgraus
    Member
    Posted 1 year ago #

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

  16. Mike
    Member
    Posted 1 year ago #

    Great! Good luck on your site!

Topic Closed

This topic has been closed to new replies.

About this Topic