Display Single Post Multiple Categories

- Written by

Last modified April 19, 2017

From the beginning...

To use advanced models of WordPress, usually it would be ideal affix a Style to each Post or Page.

This can be true and we can also apply for categories. Have you ever asked the following question:

And if the post had more Categories and we would like all of them would like to show the same task in different ways?

The usual steps:


<?php
/*
Template Name: Lessico(less)
*/
    get_header('single')
    the_content()
    get_footer('single')
?>

and print style of the template in a specific way:


<?php
function wblz_get_stylesheets() {

  $styles = 'dafault.css'; //default

  if ( is_page(array('play', 'other', 'other')) ) {
    $styles .= stylesheet_link_tag('/front-play.css', 1);
  }

  if( is_single() && in_category('playground') ) {
    $styles .= stylesheet_link_tag('/staff-onts.css', 1);
  }

}
function stylesheet_link_tag($file, $tabs = 0, $newline = true, $rel = 'stylesheet') {
  $indent = str_repeat("\t", $tabs);
  return $indent . '<link rel="' . $rel .'" href="' . get_template_directory_uri() . '/css' . $file . '">' . ($newline ? "\n" : "");
}

add_action('wblz_stylesheets', 'wblz_get_stylesheets');
?>

This is perfect if all the individual Post provides a Style for a single category... but when the Single Post is present in most categories, we can not separate each category with its own style / display mode.

So...

How to proceed to separate styles and display of Single Post belonging to more categories?

With Php we check the output HTML code of the page and let affixes in

<body> TAG

Extra steps:


<body class="cat-name">

the name of the category required, then with CSS exploit this advantage to give the correct outcome.


<?php
function wblz_get_stylesheets() {

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; //add URL

  $styles = 'dafault.css'; //default

  if ( is_page(array('play', 'other', 'other')) ) {
    $styles .= stylesheet_link_tag('/front-play.css', 1);
  }

  if( is_single() && in_category('playground') ) {
    $styles .= stylesheet_link_tag('/staff-onts.css', 1);
  }

  else if ( is_single() && (false !== strpos( $url, 'full' )) ) { //full == your-cat / & check URL

    //echo '';
    //$styles .=

}
function stylesheet_link_tag($file, $tabs = 0, $newline = true, $rel = 'stylesheet') {
  $indent = str_repeat("\t", $tabs);
  return $indent . '' . ($newline ? "\n" : "");
}

add_action('wblz_stylesheets', 'wblz_get_stylesheets');
?>

The class in the body will help us to specify that the Class X of the Post A will have a different feature of the same Post (A) seen through the Category Y.


body.cat-name{full} .code_editors {
  width: 0;
  right: 100%;
  /* . . . */
}
body.cat-name{full} .output {
  width: 100%;
  height: 100%;
  /* . . . */
}

Extra steps > END

This fully work to display each Post in CodePlay Editors Playground.

The CodePlay Code Editors Playground is a Front End - Code Editors / Live Preview / Html - CSS - JavaScript


Live example:


Cat. X == (playground)

(playground) Editors

Cat. Y == (full)

(full)

Cat. Z == (embed)

(embed)

@webbeloz

 5
ร—

Time expired..
- default (90 days)
- edit to (∞ days)
- Spamm Check

Leave a Comment

* Required: Name, eMail, Comment Emoji? : if(name) => start.eMail if(eMail) => start.Comment + Emoji (On/Off) ? a=* b=* c=31 (a + b = c) => Post a Comment enjoy(: tnx_wblz
OFF
ON
Check Url & "Not Allowed"
No Text /HTML