- This topic has 22 replies, 4 voices, and was last updated 7 years, 1 month ago by
Neil.
-
AuthorPosts
-
August 28, 2016 at 10:17 am #221894
itg
Hi Top you helped me recentley with showing different banners in different categories, the problem was solved with this code
======================
<?php
/**
* All of your custom PHP can go in this file below this commented area
* Caution: Be sure to have access to your server via File Manager or FTP in case an error occurs
* Happy editing!
*/
add_filter( ‘generate_logo’, ‘generate_category_logo’ );
function generate_category_logo()
{
if ( is_category( ‘565’ ) || in_category( ‘565’ ) ) {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo2.jpg’;
}return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’;
}
=================================================I am trying to add another new logo to another new category, how can i have a different logo for each new category, do i just keep repeating the code and changing the url to the new banner, i tried this and got white screen of death, sorry to be such a pain…. Many thanks In advance…
August 28, 2016 at 6:47 pm #221980Tom
Lead DeveloperLead DeveloperHi there,
Any chance you can paste the code that’s giving you the error?
Paste it into the editor here, highlight it and click the “code” button in the editor controls.
That will make it easier for me to see what’s going on 🙂
August 29, 2016 at 2:29 am #222046itg
`<?php
/**
* All of your custom PHP can go in this file below this commented area
* Caution: Be sure to have access to your server via File Manager or FTP in case an error occurs
* Happy editing!
*/
add_filter( ‘generate_logo’, ‘generate_category_logo’ );
function generate_category_logo()
{
if ( is_category( ‘565’ ) || in_category( ‘565’ ) ) {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo2.jpg’;
}return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’;
}add_filter( ‘generate_logo’, ‘generate_category_logo’ );
function generate_category_logo()
{
if ( is_category( ‘23’ ) || in_category( ‘23’ ) ) {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo3.jpg’;
}return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’;
}August 29, 2016 at 2:34 am #222047Neil
You don’t need to repeat the code Irv, tthis is what you want to do I think. So you can multiple conditions set to activate if found to be true, otherwise the else bit is the default which happens if none of the conditions are true
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}August 29, 2016 at 3:18 am #222050itg
Sorry, you lost me, i am useless with code
So what would be the whole code i would use, thank you
August 29, 2016 at 3:56 am #222051Neil
You should check with Tom that the following is correct, but I believe this is how you would write it.
<?php add_filter( ‘generate_logo’, ‘generate_category_logo’ ); function generate_category_logo() { if ( is_category( ‘565’ ) || in_category( ‘565’ ) ) { return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo2.jpg’; } elseif ( is_category( ‘23’ ) || in_category( ‘23’ ) ) { return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo3.jpg’; } else { return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’; } } ?>
August 29, 2016 at 4:13 am #222053itg
i used this code and got white screen
<?php
add_filter( ‘generate_logo’, ‘generate_category_logo’ );
function generate_category_logo()
{
if ( is_category( ‘1’ ) || in_category( ‘1’ ) ) {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo1.jpg’;
}
elseif ( is_category( ‘2’ ) || in_category( ‘2’ ) ) {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo2.jpg’;
}
else {
return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’;
}
}
?>August 29, 2016 at 5:06 am #222105Neil
I was assuming your original code worked? Anyway you had single quotes for the urls for the logo, they should be wrapped in “url to logo” and not ‘url to logo’
Where are you putting this code?
August 29, 2016 at 6:21 am #222129itg
I was simply copying your code neil, the code i am using is working 100% here it is
<?php
/**
* All of your custom PHP can go in this file below this commented area
* Caution: Be sure to have access to your server via File Manager or FTP in case an error occurs
* Happy editing!
*/
add_filter( ‘generate_logo’, ‘generate_category_logo’ );
function generate_category_logo()
{
if ( is_category( ‘565’ ) || in_category( ‘565’ ) ) {
return ‘http://www.mywebsite/wp-content/uploads/2016/08/logo2.jpg’;
}return ‘http://www.mywebsite.com/wp-content/uploads/2016/08/logo.jpg’;
}I am using the code in simple php plugin downloaded form the generatepress website, thank you
August 29, 2016 at 7:48 am #222145Neil
Sorry not been able to help Irv, I’ll have another look and see if I can figure it out
August 29, 2016 at 8:45 am #222174Neil
This is absolutely working Irv. I am running this on a localhost and the images are on a test site so will not always be there in case anyone is looking at this later.
You would just need to edit the if and ifelse conditions etc to suit your categories as before, as well as your own image urls of course.
As far as I can see to do what you want to, you need the first ‘if’ and then the ‘elseif’ but does not seem to work unless you have the ‘else’ at the end.
If you want to test it before adding your category conditions just change the first ‘if’ to an existing page on your site, the ‘elseif’ is any single post page, and any other page should show the ‘else’ image.
Hope that helps you sort it out. You can of course add further ‘elseif’ as needed.
add_filter( 'generate_logo', 'generate_category_logo' ); function generate_category_logo() { if ( is_page('sample-page')) { return 'http://gwwtest.com/gk/wp-content/uploads/2013/02/cropped-galwaykennels_header6001.jpg'; } elseif (is_single()) { return 'http://gwwtest.com/gk/wp-content/uploads/2011/07/Teas-up.jpg'; } else { return 'http://gwwtest.com/gk/wp-content/uploads/2013/02/galwaykennels_header6001.jpg'; } }
August 29, 2016 at 8:50 am #222177Neil
Just to add, this is working for me. Try it as I suggest without the categories, keeping it simple first, then try adding in the category conditionals as there may be a problem with those as coded so far?
August 29, 2016 at 8:58 am #222180Ramesh Srinivasan
My 2 cents:
And beware of those curly quotes.. You may have to replace it with normal quotes, as in Neil’s latest reply.
Not an expert in PHP but those special quotes can cause problems in any scripting/programming language in general.
August 29, 2016 at 9:35 am #222192Neil
You know what? There was a syntax error in there somewhere, the ‘category number’, the apostrophes were not right somehow, I retyped those after adding a couple of sample categories and it is working with those now for me.
add_filter( 'generate_logo', 'generate_category_logo' ); function generate_category_logo() { if ( is_category( '2' ) || in_category( '2' ) ) { return 'http://gwwtest.com/gk/wp-content/uploads/2013/02/cropped-galwaykennels_header6001.jpg'; } elseif ( is_category( '3' ) || in_category( '3' ) ) { return 'http://gwwtest.com/gk/wp-content/uploads/2011/07/Teas-up.jpg'; } else { return 'http://gwwtest.com/gk/wp-content/uploads/2013/02/galwaykennels_header6001.jpg'; } }
August 29, 2016 at 9:40 am #222193Neil
As Ramesh mentioned and found here
Works – <?php echo ‘<p>Hello World</p>’; ?>
Doesn’t work – <?php echo ‘<p>Hello World</p>’; ?>
The latter comes from a normal text editor, so that was the issue all along, one or several ‘slanty’ quotes got involved and broke the code. Which was then repeated by copying the original error.
-
AuthorPosts
- You must be logged in to reply to this topic.