• Resolved happik

    (@happik)


    Dear all.

    I have the single.php file and I would like to assign a post format. It is usually done with the help of the code:
    get_template_part('content', get_post_format()),
    but I don’t want to inject any code, only display the post in corresponding post format (i.e. standard/aside/gallery/link).

    Does the WordPress enable it in other way?

    Thank you for any useful tips.

    • This topic was modified 6 years, 10 months ago by happik.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    you don’t have to use get_template_part(). You can just encode the logic and formats directly inside single.php.

    Thread Starter happik

    (@happik)

    Ok, so my php-pages are content-aside.php, content-gallery.php, content-link.php. They work perfectly in index.php, search.php, archive.php (I use the code get_template_part('content', get_post_format()) there).

    What logic should I write to the single.php to force to use the content-gallery.php with corresponding CSS styles in case I choose the gallery post format?

    Thank you.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Why not use that same link in single.php?

    I guess I don’t understand what you’re trying to do.

    Thread Starter happik

    (@happik)

    I have created the file content.php that contains now:

    <article class="post <?php if (has_post_thumbnail() ) { ?>has-thumbnail <?php } ?>">
    			
    		<!-- post-thumbnail -->
    		<div class="post-thumbnail">
    			<?php if (is_single() ) {?>
    					<?php the_post_thumbnail('banner-image');
    					
    			} else {?>
    					<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('small-thumbnail');
    					}?></a>
    		</div><!-- /post-thumbnail -->
    
    			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    			
    				<p class="post-info"><?php the_time('j. n. Y G:i'); ?> | od <a href="<?php get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author()?></a> | Publikováno v 
    
    					<?php
    						$categories = get_the_category();
    						$separator = ", ";
    						$output = '';
    
    						if($categories) {
    							foreach ($categories as $category) {
    
    								$output .= '<a href=" ' . get_category_link($category->term_id) . ' ">' . $category->cat_name . '</a>' . $separator;
    
    							}
    
    							echo trim($output, $separator); 
    						}
    					?>
    
    				</p>
    
    				<?php if (is_single() ) {
    					the_content();
    
    				} elseif ( is_search() OR is_archive() ) {?>
    					<p>
    					<?php echo get_the_excerpt(); ?>
    					<a href="<?php the_permalink(); ?>">Číst dál&raquo;</a>
    					</p>
    				
    				<?php } else {
    					if ($post->post_excerpt) { ?>
    					<p>
    					<?php echo get_the_excerpt(); ?>
    					<a href="<?php the_permalink(); ?>">Číst dál&raquo;</a>
    					</p>
    					<?php } else {
    						the_content();
    					}
    
    				} ?>
    
    </article>

    and I would like to remove the undesirable if-conditions is_single() in the content.php. I would like to create specific file single.php that will correspond the format of the post chosen in the WordPress backend.

    Unfortunately I can’t do it now because I don’t know the suitable code in single.php to respect the type of the post.

    If I write the line get_template_part('content', get_post_format()) to the single file then the condition is_single() will be called automatically “in the single.php” (I know that I am in single file so it is easier write there the code to respect the format directly).

    • This reply was modified 6 years, 10 months ago by happik.
    • This reply was modified 6 years, 10 months ago by happik.
    • This reply was modified 6 years, 10 months ago by happik.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    In single.php, you’d do something like

    if ( 'gallery' == get_post_format() ) {
       your stuff goes here 
       } else {
       whatever you do if it's not a gallery 
       }

    The idea of the content-* files is that single.php is kept relatively agnostic to the post formats — you farm out the differences through the get_template_part function to various content files.

    Thread Starter happik

    (@happik)

    So if I understand well – the function get_post_format() should return the string standard or aside or gallery or link based on the chosen format of the post?

    So this code should work:

    if ( 'gallery' == get_post_format() ) {
    
       <article class="post post-gallery">
    	<h2><?php the_title(); ?></h2>
    	<?php the_content(); ?>
       </article>
    
    } elseif ( 'aside' == get_post_format() ) {
    
       <article class="post post-aside">
    	<p class="mini-meta"><?php the_author();?> @ <?php the_time('j. n. Y G:i')?></p>
    	<?php the_content(); ?>
       </article>   }
    
    } elseif ( 'link' == get_post_format() ) {
       
       <article class="post post-link">
    	<p class="mini-meta"><?php the_author();?> @ <?php the_time('j. n. Y G:i')?></p>
    	<a href="<?php echo get_the_content(); ?>"><?php the_title(); ?></a>
       </article>
    
    } else {
       ... 
       }

    and then I should only set the CSS of the classes post-link, post-aside, post-gallery.

    Moderator bcworkz

    (@bcworkz)

    When the format is ‘standard’, it is treated as no format is assigned, so get_post_format() will return false in that case. If another format is assigned, you are correct, the format slug is returned, like aside or gallery. For this function to work, the post_type registration must declare support for post-formats. Formats are simply another taxonomy, like categories and tags. Adding format support is the same as adding support for any other taxonomy. You could use the taxonomy function get_the_terms() to determine the format if you wanted to.

    Using get_post_format() with no arguments requires the call be made within a standard WP Loop. If all of these items are addressed, it appears your code should work. If it’s not, try checking your error logs for clues, or define WP_DEBUG as true in wp-config.php.

    Thread Starter happik

    (@happik)

    Thank you all for all the information. Now it will take some time to check and debug it 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘assign post format to single.php without get_template_part’ is closed to new replies.