Wordpress If Category statement then show this layered image - wordpress

I have a wordpress blog that has a custom post-type with its own set of categories. There's a similar code in my template that uses a function that searches for different post formats in the blog section and show specific icons for that specific format.
Ex: if(has_post_format('audio')) {return 'ICON IMAGE';}
I would like to do the same thing with the custom post-type's categories. Is there a way where I can search through the custom post-type and by category show different icons?
Ex: if (is_category('Case Studies') {return.......
I tried it that way, but it didn't work out. I think it has something to do with the custom post-type. Do I need to attach the custom post-type to the category somehow?

I needed something similar for four custom post type categories and worked this out, I'm a PHP novice so this may not be the best way but it works for me.
Outside the loop.. I used 'single_term_title to get the name of the custom post type category and assigned it to a variable '$current_term', then used if / else if statements...
<?php $current_term = single_term_title("", false);
//Cat name 1
if($current_term === 'Name 1') {
echo 'something';
}
//Cat name 2
else if($current_term === 'Name 2') {
echo 'something';
}
//Cat name 3
else if($current_term === 'Name 3') {
echo 'something';
}
//Cat name 4
else if($current_term === 'Name 4') {
echo 'somehting';
}
//Do nothing
else {
echo '';
}
?>

Related

Print "custom label" for indicating type of post in the front end for Wordpress

I have a blog with different custom post types ("books", "interviews", "recipes", "events", etc...). those are all appearing in the home page with same format like a grid. I would like to print in the front end a "label" customized possibly, representing the kind/content of post.
For example:
if the post is a CPT "Book", I want to show in the grid cell "looking for a book?"
if the post is a CPT "Recipe", I want to show on same position, for specific post "hungry?"
etc...
Can you maybe help me in this? I guess I need some PHP code and set it with Elementor, but I am not a developer... :(
Thanks for any help.
Mario.
I have been asked in comment to put a screenshot. this is a fake grid taken from internet (I know, ugly layout), presenting in descending order by date all posts, very different in domain (different custom post types), which I am able to do it. What I need is, depending by Post Type, to add a slogan like "watch the movie" or "hungry?" or "Interview with...", a static string totally dependent by the type of CPT.
fake sample from internet
Further integration to explain the context.
See the current home page of my site: click here
You see two "posts" in a grid (3 columns, published with "post" widget in elementor and a custom skin. This custom skin is linked to a "loop" template created with "ele custom skin". As you see by the pic, you have one post which is a recipe (custom post type "Recipe") and one is a book (custom post type is book). But here I can eventually find also a standard post. Now, when you see the "red dot", I would like to put a word, which is directly dependent by the post type:
if "recipe" --> "Hungry?"
if "book" --> "our book reviews"...
etc...
as a sample I have in this link click here for each loop in the grid, called using "shortcode" widget
[helloworld]
and coded in snippets plugin following portion of php code
function HelloWorldShortcode() {
return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');
Here is the code, from this code shortcode will be [vkh_display_post_tagline]
/**
* Custom shortcode to display the tagline by the post type.
*/
/**
* Custom shortcode to display the tagline by the post type.
*/
function vkh_display_tagline_by_post_type() {
global $post;
if ( ! $post || empty( $post ) ) {
return;
}
// Uncomment the next line for debugging and to know if we're getting post type of not.
// return $post->post_type;
$heading = '';
switch ( $post->post_type ) {
case 'recipe':
$heading = __( 'Hungry?', 'your-text-domain' );
break;
case 'book':
$heading = __( 'Our book reviews', 'your-text-domain' );
break;
// Add more cases following above examples.
default:
// If you have to assign anything default when a case doesn't match
// then write that here.
$heading = '';
break;
}
// When heading is not empty return heading within the html tag.
if ( ! empty( $heading ) ) {
return '<h4 class="post-tagline">' . esc_html( $heading ) . '</h4>';
}
}
add_shortcode( 'vkh_display_post_tagline', 'vkh_display_tagline_by_post_type' );

How to display a specific text with the same first slug in two different pages?

I have some pages with two slugs,
For the first one they have both property,
For the last one it could be status or features .
Something like :
http://www.mywebadress.com/property/status
http://www.mywebadress.com/property/features
I want to display "Text 1" if i'm on the status page and "Text 2" if i'm on the features page. How can i do it ?
I've to use a slug function of wordpress like this code but for slugs ?
Thanks
<?php
if(is_page()){
echo 'Text 1';
} elseif(is_page()){
echo 'Text 2';
} else {
////
}
?>
You should probably just create subpages. This will give you the slug system you want and it will be easier to manage each page individually. The codex has information on how to do this here: https://codex.wordpress.org/Pages#To_create_a_subpage

Using the same template for listing subcategories and posts

I'm trying to use the same template (category-slug.php) to do the following:
Check if category has children - we show subcategories
otherwise - we display all posts that belong to a specific category
So what's the correct way to do so? How many templates I need for this?
You can do it all in the same template file like so:
<?php
// list child categories
$cat_id = get_query_var('cat');
$catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id);
if (get_categories('parent=' . $cat_id)) {
echo $catlist;
} else {
// normal loop
}
?>

How do you assign a specific order for a category in Wordpress?

I am having a small problem which I believe to be related to a syntax issue.
I have a specific script for home which calls the bloginfo("name") for the page title then an else statement below which titles pages based on the name of the first category associated them and it works fine:
else{
$category = get_the_category();
echo $category[0]->cat_name;
}
However, I also have a default "home" category for most posts(with a few exceptions) and I do not want this category to be picked up as the page title if it happens to come first in the list of categories associated for this page.
There are two ways I can think of making this possible but I do not know how to code them. The first would be to exclude the home category in the code above but don't know how to implement it. The second would be to somehow force wordpress to always add the category "home" after the first chosen category for a particular post.
I hope that this is clear. Anyone come up with a basic solution?
Many thanks.
This plugin does most of what you want out of the box:
http://wordpress.org/extend/plugins/sem-opt-in-front/
Alternatively, do something like:
function the_main_topic_link($link = '', $id = '')
{
if (!$id || $id != get_option('default_category')) return $link;
if (get_option('show_on_front') == 'page' && get_option('page_on_front')) {
if ($blog_page_id = get_option('page_for_posts')) {
return apply_filters('the_permalink', get_permalink($blog_page_id));
} else {
return $link;
}
} else {
return home_url('/');
}
}
add_filter('category_link', 'the_main_topic_link', 10, 2);
function pre_get_posts_redirect_main_topic($q)
{
global $wp_the_query;
if ($q !== $wp_the_query) return;
if ($q->is_category(get_option('default_category'))) {
wp_redirect(home_url('/'));
die;
}
}
if (!is_admin()) add_action('pre_get_posts', 'pre_get_posts_redirect_main_topic');
It'll treat linking to your default category as linking to your front page.

How to make a plugin to count visitors for posts under specific category

How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.

Resources