I want to hide this specific in the blog and other page I have this code :
<?php if (is_page(array ('2','4','6','8','10','12'))) : ?>
<?php else : ?>
<?php include('stats.php');?>
<?php endif; ?>
it won't show on 2,6,8,10,12 which are the pages but it shoes in 4 which is the blog please help.
Assuming 'blog' is a category, and it's ID is 4 and you don't want to include 'stats.php' when viewing that category's archive page:
<?php if (!is_page(array('2','4','6','8','10','12')) && !is_category('4')) {
include('stats.php');
} ?>
Otherwise, refer to the documentation for Wordpress conditional tags.
Related
I want to display different sidebars in wordpress for different categories and for posts belonging to these categories. I want to make this happen within one expression.
<?php if (is_category('25')) : ?>
<p>Sidebar for Category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
This works but when I try to display the custom sidebar for category page and single posts from cat25 at the same time with:
<?php if (is_category('25')) || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
nothing happens.
I have a learning deficit and a hard time with logic. But still I try and I keep improving.
Please consider this when giving me an answer.
I made a simple mistake. I closed to early with the ) and used the ) one more time at the end.
At least this might help someone coming from a search engine.
This is the working code:
<?php if (is_category('25') || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
If someone has a simpler solution or any changes according to best practices please still feel free to give your input :-)
I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease.
I would like to have the ability to add a 'new' or 'featured' icon based on its corresponding 'new' or 'featured' category or tag - either will do.
I have added these categories but they do not appear in the code when output and so I cannot target them.
How would I be able to perform the action:
If a post thumbnail has category of 'new' add the class 'new' so I can then target and style - repeating for each category.
I found this which I think is similar but does not work
There will be multiple categories displaying on the archive page, but I want to style only the previews that have a certain category - I do not want to style the individual post page.
Unfortunately my php skills are limited
Thanks
$post = $wp_query->post;
if ( in_category('new', $post->ID) ) { ?>
<body <?php body_class('new'); ?>>
<?php
}
elseif ( in_category('featured', $post->ID) ) { ?>
<body <?php body_class('featured'); ?>>
<?php
}
else { ?>
<body <?php body_class('class-name-generic'); ?>>
<?php
}
?>
here you go get_the_category($post->ID); will return the array of categories of that post you need to loop through the array
$category_detail=get_the_category('4');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
My theme has a black logo for home page and white logo for pages and posts. It shows a white logo as it expects a featured image on pages and posts and then the logo itself is visually good looking overlaying the image.
This works great on posts (as I put a featured image on every post), but on pages I want to show a black logo, as I don't want any featured images.
I have put this code in my page.php file:
<?php
if(is_page(3)) {
get_header('BLACK');
}
else {
get_header();
}
wp_head();
?>
In this case I created a header-black.php file for my page with ID 3. If the page is not ID 3 it just falls to the normal theme header. All good up to here, but now I have a dilemma:
What do I have to do if I want a header-black.php for my ID page 3, 4, 5 etc.? If I add the same code with different page ID below:
<?php
if(is_page(3)) {
get_header('BLACK');
}
else {
get_header();
}
wp_head();
?>
<?php
if(is_page(4)) {
get_header('BLACK');
}
else {
get_header();
}
wp_head();
?>
it shows both logos on ID 4 page (the black AND the white)? The logo is still okay on the ID 3 page though. There will be only a few pages on my website (mostly posts) - just to point that out.
I have never used WordPress but it seems is_page accepts array.
https://developer.wordpress.org/reference/functions/is_page/
if(is_page(array(3,4,5))) {
get_header('BLACK');
}else {
get_header();
}
wp_head();
If you have page ID 3, 4, 5 these types of condition will be ok, but if you have 10, 20 pages then, what will you do ? You will keep on adding conditions ? So, best option in such cases is to create page templates, and add conditions.
Create any page templates, eg. black-logo-template.php
<?php
if(is_page_template( 'black-logo-template.php' )) {
get_header('BLACK');
}
else {
get_header();
}
wp_head();
?>
PHP "OR" Operator: http://php.net/manual/en/language.operators.php
<?php
if ( is_page(3) || is_page(4) ) :
get_header('BLACK');
else :
get_header();
endif;
wp_head();
?>
You could also pass an array to the is_page function: https://developer.wordpress.org/reference/functions/is_page/
<?php
if ( is_page([3,4]) ) :
get_header('BLACK');
else :
get_header();
endif;
wp_head();
?>
& (downvote received?) if anyone's wondering why I posted the OR operator first, it's because the question presents code which doesn't demonstrate an understanding of how PHP works.
So learning PHP operators & the basics of procedural programming would be my first recommendation for this user.
We are setting up a custom blog layout override for a Joomla site. I am trying to display the sub categories articles under their category headings.
For example the following structure:
Main Cat
- Sub Cat
-- Article 1
-- Article 2
- Sub Cat
-- Article 3
This layout is how the articles should display on the main blog override. For example going to Main Cat would display the above layout.
The subcategories are included in the default way with the following:
<?php if (!empty($this->children[$this->category->id]) && $this->maxLevel != 0) : ?>
<div class="cat-children">
<?php if ($this->params->get('show_category_heading_title_text', 1) == 1) : ?>
<h3> <?php echo JText::_('JGLOBAL_SUBCATEGORIES'); ?></h3>
<?php endif; ?>
<?php echo $this->loadTemplate('children'); ?> </div>
<?php endif; ?>
In the child override the title is included then we would like to display the articles from that child category.
I have tried multiple different versions of getting a for each to work for the sub articles but cannot get it to work.
For example:
<?php foreach ($this->child->children[$child->id] as $key => $item) : ?>
1
<?php echo $item->title; ?>
<?php endforeach; ?>
I am having some problems trying to get a custom page to display on my site. What I want is to create a page where the client can edit the content in Wordpress. When I add the page using the Wordpress "add new page" it doesn't show, but if I create a page in .php with the same content, it works fine. This is what I did:
First I created a blank .php template called lefthome.php using the following code:
<?php
/*
Template Name: Left Template
*/
?>
<?php get_template_part( 'loop', 'page' ); ?>
I then went into Wordpress, clicked on pages > add new. I gave my page a title and added the content I wanted to appear on the page. Then from the template option in the page attributes I choose the template I had earlier created (Left Template) and clicked update.
I wanted this template to appear on the homepage, so I tried to add it to the homepage template I had created using the following code:
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php include (TEMPLATEPATH . '/lefthome.php'); ?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>
The header, slider.php and footer all show fine. The contents of the lefthome.php do not appear. I do not know how to get it to show. The only way I have got it to show is to paste the contents into the lefthome.php template. I don't want to do this though, as the client will not be able to edit the contents themselves.
I hope someone can help me.
Thanks
You won't be able to display the content of a page just by calling directly its template file. You need to query the page itself. In that matter it won't matter what template you've already assigned to it anymore.
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php
global $post;
$page_id =5; // 5 is the id of the page we want to present with Left Template.
$post = get_post( $page_id );
setup_postdata( $post );
include locate_template( 'lefthome.php' );
wp_reset_postdata(); // we are done with that. reset the query so the rest don't mess up
?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>