0

First let me know you that I have made a blog before and I have customize it according to the post format but I have customize it on the blog main page i.e. index.php.

Now I am making a another blog and I want to customize it by post format but this time I want to customize the post on the read page i.e. single.php (according to post type).

Is it possible to make it or we can only customize the post format on index.php.

Update:

Sorry:-

Its post format not post type

  • What do you mean by "customize". Do you want different HTML code for different post formats? – Krzysiek Dróżdż May 30 '18 at 17:47
  • @KrzysiekDróżdż Yes and sorry for the late reply. – Owaiz Yusufi May 31 '18 at 5:07
1

Yes, it is possible. The easiest and very readable way would be to do this like TwentyX themes do - using get_post_format and get_template_part functions.

Let's say that your current single.php file looks something like this:

<?php get_header(); ?>
... 
<?php while ( have_posts() ) : the_post; ?>
<article ...>
    ...
</article>
<?php endwhile; ?>
...

Just change it to:

<?php get_header(); ?>
... 
<?php
    while ( have_posts() ) : the_post;
        get_template_part( 'content', get_post_format() );
    endwhile;
?>
...

And move this part:

<article ...>
    ...
</article>

to file called content.php;

Now, this will be the default HTML for your post. If you want to modify the HTML code for... let's say quote format, then you'll have to create new file called content-quote.php (so the rule here is (content-{post_format}.php).

Why and how does it work?

get_post_format returns the post format of a post.

get_template_part on the other hand makes all the magic ;) Citing from Codex:

Provides a simple mechanism for child themes to overload reusable sections of code in the theme.

Includes the named template part for a theme or if a name is specified then a specialised part will be included. If the theme contains no {slug}.php file then no template will be included.

The template is included using require, not require_once, so you may include the same template part multiple times.

For the $name parameter, if the file is called "{slug}-special.php" then specify "special".

| improve this answer | |
1

Sure, you can simply grab a class of the post, and then style it with your own CSS.

The following code is taken from this page of the WordPress Codex: Post Class

This function returns all classes of the post, it can appear in home.php and/or in single.php for example.

// PHP single.php template code
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

This HTML/PHP markup in your theme's loop creates the following output:

//HTML Output by WordPress
<div id="post-4564" class="class-name post-4564 post type-post status-publish format-standard hentry category-news">

As you can see, it gives you the class "format-standard". The following classes can be output, depending on the post format:

aside, gallery, link, quote, status, video, audio, chat

So, if you want to style your single.php differently, look these classes and grab them by CSS:

/* CSS */
.format-standard {
    background: url('images/lonesome-background.jpg')top center no-repeat;
}

.format-audio {
    background: url('images/funky-music.jpg')top center no-repeat;
}

.format-gallery {
    background: url('images/super-wide-gallery.jpg')top center no-repeat;
}

...and so on

You should have a look at the Codex. Hope this helps.

| improve this answer | |
  • Sorry for the late reply I want to change the html according to the post format – Owaiz Yusufi May 31 '18 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.