Wordpress Category vs. Sub-category Conditional - wordpress

I'm trying to develop two different templates; one layout for a category listing, and a separate layout for subcategories.
I believe it'd require a conditional statement detecting if the current category is a category or subcategory.
I'm familiar with:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
But I need this to be more dynamic and handle any category or subcategory without hard coding the cat ID number.
Any help would be appreciated, thank you in advance.

You can use get_category() (WordPress Codex) to get all information about a category. Using this, you could create a custom conditional function in your functions.php like this:
function is_subcategory ($catid) {
$currentcat = get_category($catid);
if ( $currentcat->parent ) {
return true;
} else {
return false;
}
}
Now you could use this function, to determine if a category has a parent category, and therefore is a subcategory.
Unfortunately I don't know the context of your posted code. You'll somehow need to get the category id. Do you use the loop the walk the categories or a custom sql-query?
If the answer is not helping, maybe you could add some surrounding code to your question.

Related

Conditional custom post type while post ID is - WordPress

How to add content to specific custom post type page?
I am using this code
is_singular()
I wanted to add content to custom post type, but for specific page only.
I just figured it out using get_the_id.
The code is something like this
is_singular('custom-post-slug') && get_the_id() == 123 ): ?>
"custom-post-slug" is the slug of the custom post type
"123" is the page ID
#user2415803 it a bad practice to hardcode ID like this please consider using custom fields or some other checks to get the desired ID.
Can you please provide more information? If you are trying to add content for specific custom post type just use this :
if ( get_post_type() === 'your_post_type_name' ) {
// add the content
}
Please note that you can extend the if condition to be for specific page and so on. Please let me know if you have further questions.
In these cases you can i always add single-{custom-post-type}.php in the theme and define specials html structure for that like following code:
<?php get_header(); ?>
<div class="single-page-customtype">
Hello Single
</div>
<?php get_footer(); ?>

make wordpress posts change content dynamically

Is there a way to customise a wordpress blog post so that it changes slightly based on the User?
eg. when showing a post around Salary and Bonuses on the company blog, I'd like to customise some of the text based on the employee level (director, Executive etc) - for example:
a paragraph of text about pension contribution should only appear to
directors reading the page
the bonus amount should change based on employee level.
I don't want to create several pages per category (eg. one page for the directors, one for the execs etc) - I would like to have just one blog post with some variable fields?
The codex has a great function outlined, current_user_can
http://codex.wordpress.org/Function_Reference/current_user_can
As well as WP_User_Query
http://codex.wordpress.org/Class_Reference/WP_User_Query
You can use both of these to accomplish your goal.
I needed to do something similar, I came up with custom roles based on the plugin "Capability Manager Enhanced".
In a file you can check the roles like:
<? if ( is_user_logged_in() ) : ?>
<?
global $current_user;
get_currentuserinfo();
$roles = $current_user->roles; //$roles is an array
if ( is_user_logged_in() && in_array("custom_role", $roles) ) {
echo('WORK HARDER!');
} else {
echo('NO CAKE FOR YOU!');
}
?>
<?php endif; ?>

Wordpress: children posts from multiple categories

Going to "mysite.com/category/my_city/restaurants/" I can display all the posts that are under category "my_city" AND under category "restaurants".
Let's assume that restaurants category has several childs (thai, italian, etc): how can i make a list of all children subcategories of restaurants that are ALSO and ONLY under my_city category?
For example "mysite.com/category/my_city/thai/"
For the moment this is the code in my category.php file:
<?php
$this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1;
$this_category = get_category($this_cat);
if ( $this_category->parent ) { $this_cat = $this_category->parent; }
wp_list_categories('title_li=&child_of=' . $this_cat . '');
?>
The results is a list like "thai, italian, etc" (that apparently is good), but the links are all pointing to something like "mysite.com/category/restaurants/thai/", showing ALL restaurant, not only those under my_city.
Obviously I'm not catching the other category and don't know how to do the right array.
Thanks a lot in advance for your help.
https://gist.github.com/siddhartanaranjo/6073854
in this code get the cat of the cat-page and display subcats and parent with a simple subcategory-loop
or this in your template of restaurant-page
https://gist.github.com/siddhartanaranjo/6128695

Wordpress: Show post if it is in two specific categories

I am trying to get the index-page of a Wordpress-blog show some very specific posts. As far as I understand i need to use a standard loop in order to make sticky posts work, so custom queries is out of the question. (Correct me if this is wrong.)
All posts are put in a main category (Eg. "Make-Up") In addition, posts that should show on the front page gets an additional category "Frontpage".
The current loop outputs all posts, regardless of category. And styles certain categories differently. An example would be the video-category which is only shown by getting an embed code from a custom field in the post.
<?php elseif (in_category('20')) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
I need to remove all posts not in the category "Frontpage" while still being able to control how posts are being shown.
Earlier i used a filter to control the main loop:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '20 27');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
However this would cause my geomashup-plugin to break as it probably uses the same loop?
My current proposal for a solution would be to do something like this, plus functioning code:
<?php elseif (the post is in BOTH category 20 and 27)) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
<?php else : ?>
<div style: display: none;></div>
However i am unsure about how make a condition demanding the post to be in two categories, and i realise this is a terribly dirty fix.
Any tips or pointers as to how i could solve this would be greatly appreciated :)
Front page can be seen here: http://parfymelle.brandbase.no
For anyone wondering i solved it by including the geotagged posts-category (the shop locations) in the filter for the main loop, and then using a php if in the index.php to hide posts from that category. Dirty, but works, i guess.

Drupal 6: pre-defined variable for amount [count] of custom type items

I'm a drupal newbie...
I researched but couldnot find :/ is there any predefined variable that gives my CCK field value count?
for example; I have field_logo_sponsor and I need to display all logo items. Now I have 5 item
<?php print $node->field_logo_sponsor[0]['view'] ?>
<?php print $node->field_logo_sponsor[1]['view'] ?>
<?php print $node->field_logo_sponsor[2]['view'] ?>
<?php print $node->field_logo_sponsor[3]['view'] ?>
<?php print $node->field_logo_sponsor[4]['view'] ?>
it is stupid to use it that way :/ if there is any count variable for that, I will just create a loop for that and display them in a for or while loop
Appreciate helps! thanks a lot!
How about:
<?php
foreach($node->field_logo_sponsor as $logo_sponsor) {
print $logo_sponsor['view'];
}
?>
Also count($node->field_logo_sponsor) should return you the number of items.
Sidenote: never use
foreach($node->field_logo_sponsor as $logo_sponsor) {
print $logo_sponsor['value'];
}
Even if that calue contains what you want, and the view does not contain the HTML you want. value is unescaped, meaning, it can (and therefore will, at some point) contain stuff like XSS.

Resources