Wordpress - different logo on pages - php

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.

Related

How to insert page/post specific script in header and footer in WordPress?

How to insert page/post specific script in header and footer in WordPress?
I need to insert the script on a specific page or post. For Example, I will insert different codes in different pages/posts. The codes will not appear in all the pages/posts.
You can use is_page() function:
is_page( $page = '' );
$page
(int|string|array) (Optional) Page ID, title, slug, or array of such.
For example if your page id is 1954
if( is_page( $page = 1954 ) ){
your specific code for this page here..
}
You can Do it via Slug of post or page or id of post/page using...
1. is_single() for post
2. is_page() for page
you can add or functions also if you want to add that code in multiple pages but specific pages only.
here is code...
if (is_page('home') || is_page('contact') || is_page('45')
Example 1....
<?php if (is_page('home')): ?>
<!--home page add custom JS-->
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/Scripts/customJS.js"></script>
<?php endif; ?>
Example 2...
<?php if (is_page(346) ):?>
<!-- Google Analytics Content Experiment code here -->
...
<!-- End of Google Analytics Content Experiment code here -->
<?php endif; ?>

Wordpress function get_post_gallery() does not register post's gallery

I want to extract the images contained in a gallery block while maintaining their correct order. Since get_children() and get_attached_media() do not seem to register when the image order is changed in wp-admin, I'm trying to use get_post_gallery() instead.
My problem is that the function returns false, even though the post does have a gallery.
I tried both the example and the plain usage from Codex. Currently, my entire single.php looks like this:
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if ( get_post_gallery() ) :
echo get_post_gallery();
else :
echo (the_ID() . " has no gallery.");
endif;
endwhile;
endif;
?>
… which results in "ID has no gallery" every time.
However, the output of print_r($post->post_content); does include the following, which seems to confirm that there is in a fact a gallery:
<!-- wp:gallery {"ids":[80,81,82]} -->
<figure class="wp-block-gallery columns-3 is-cropped">
<ul class ="blocks-gallery-grid">
<!-- … -->
I'm also attaching a screenshot from wp-admin to make sure I don't misunderstand what constitutes a gallery.
get_post_gallery() works only for native galleries which have been created in classig WYSIWYG editor. You can find more about it there. Hovewer, if you create Gallery via Gutenberg, it create the whole HTML code instead of classic editor gallery which create shortcode like this: [gallery ids="400097,400052,400051"] . Functions get_post_gallery(), get_post_galleries() and get_post_gallery_images() works only on classic galleries added via native shortcode.
Are you only trying to display the gallery block as opposed to all content? If so, maybe try this?
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if (has_block('gallery', $post->post_content)) {
echo 'yes, there is a gallery';
$post_blocks = parse_blocks($post->post_content);
foreach ($post_blocks as $post_block){
if ($post_block['blockName'] == 'core/gallery'){
echo do_shortcode( $post_block['innerHTML'] );
}
}
}
// if there is not a gallery block do this
else {
echo 'no gallery';
}
endwhile;
endif;
get_footer();
?>
I got some inspiration from reading this post btw.

Home Template Loop customization - should fetch content based on post template

In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!

How to make and exception using "if" in a wordpress php page

I am using a wordpress theme that has a specific layout feature for the blog template anytime "category" is used.
The blog template displays the date in a ribbon graphic and there is additional control to turn it on or off through an additional "epanel" in wordpress.
I have 2 categories (Blog and Services). I would like to remove the DATE feature from being display only in the "Services" category.
It is added like this :
<?php if ( get_option('sky_postinfo1') ) { ?>
<div class="post-date">
sky_postinfo2 would mean that is off.
Each page has an H1 that echos the page title like this:
<h1 id="page-title"><?php echo esc_html( $et_page_title ); ?></h1>
Is there a way to write an exception to remove this only for the "Services" page?
Something like :
<?php
if {
( $et_page_title = ('Services') get_option('sky_postinfo1');
} else get_option('sky_postinfo2');
?>
I am sure I absolutely butchered the last part, but I am looking for direction if possible. Any help greatly appreciated. Is doing it this way practical or am I way of base?
mhhhh, assuming the $et_page_title variable has this value on this page...
<?php if ( get_option('sky_postinfo1') && $et_page_title != 'Services') { ?>
<div class="post-date">

Hiding a DIV in sidebar in Wordpress

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.

Categories