displaying content of custom post type

I have a following code for the custom post type, and I want to display the content of it inside the loop.

  $args = array(
            'post_type'       = 'gallery',
            'posts_per_page'  = -1,
            'paged'           = $paged,
            'supress_filters' = false,
        );

        // Tax query
        if ( $gallery_filter_parent ) {
            $args['tax_query'] = array( array(
                'taxonomy'  = 'gallery_cats',
                'field'     = 'id',
                'terms'     = $gallery_filter_parent
            ) );
        }
        // Get post type == gallery
        query_posts( $args );
        // Loop through gallery items
        while ( have_posts() ) : the_post();
            get_template_part( 'loop', 'gallery' );
        endwhile; ?

Topic post-content custom-content loop posts custom-post-types Wordpress

Category Web


What do you have in loop-gallery.php? That's the file which will show the content of the gallery post.

If you haven't created a loop-gallery.php, you should create a file with that name and put it in the root theme folder. In that file you could put something like the following to show the content:

<div>
  <h2><?php the_title(); ?></h2>
  <div><?php the_content(); ?></div>
</div>

You could obviously put something more complex in that file, if you however don't need more than that and you're not reusing the snippet elsewhere in your theme you could simply do the following change to your code to get the same result:

while ( have_posts() ) : the_post();
  <div>
    <h2><?php the_title(); ?></h2>
    <div><?php the_content(); ?></div>
  </div>
<?php endwhile; ?>

Also, have you recently registered your custom post type? If so you may have to reset your permalinks (although that shouldn't have anything to do with this specfic use case. To reset your permalinks, simply visit Settings > Permalinks and hit save. That will resave the permalinks on your site, including the new permalinks for the custom post type.


Your code requires following things

  • loop-gallery.php (solution: simply copy and pest+rename loop-content.php into loop-gallery.php, it really varies from theme to theme. Different theme have different structure, if you are using a free theme from Wordpress directory, tell the name)
  • A custom taxonomy named gallery_cats.
  • change post_per_page value into 10 (number of posts you want to be displayed.)

have you tried using this?

<?php the_content() ?>

also, you're code assumes there is a template part called 'gallery content'. does the file exists?

simple testing method would be:

// Loop through gallery items
    while ( have_posts() ) : the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <div><?php the_content(); ?></div>
    <?php endwhile; ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.