Using if is_page() Wordpress conditional statement - wordpress

I'm trying to have different pictures on every one of my pages built on wordpress.
So I have the following in my index.php file, archive.php file, page.php file, etc:
<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic; ?>" alt="page1" id="mainPageImg" />
Now, in my page.php file, I have the following:
<?php
// TOP PICTURE DEFINITIONS
if ( is_home() ) {
$toppic == 'page1.png';
}
if ( is_page('articles') ) {
$toppic == 'page2.png';
}
?>
How come this does not work? I tried it with one equal (=) sign...
EDIT: If I define $toppic at the top, for example, in the index.php file as follows:
<?php $toppic = 'page1.png'; ?>
Then it works. So therefore, it must be something that has to do with the conditional if is_page/is_home statements. Any ideas?
Thanks!
Amit

Okay, I found the answer.
This is what needs to be done. For the articles (blog) page, at the top section you need to place the following:
<?php // TOP PICTURE DEFINITION FOR ARTICLES PAGE
if ( is_home() ) {
$toppic = 'page1.png';
}
?>
Then, in your page.php file, you can control the picture at the top for all other pages (except 404, where you'd need to put a is_404() in your 404.php. So this is what it looks like:
<?php
// TOP PICTURE DEFINITIONS
if ( is_page('english') ) {
$toppic = 'page1.png';
}
if ( is_page('aboutus') ) {
$toppic = 'page1.png';
}
if ( is_page('newspaper') ) {
$toppic = 'page1.png';
}
else {
$toppic = 'page1.png';
}
?>
And finally, in order to implement this, use the following HTML/php syntax:
<img src="<?php bloginfo('template_url'); ?>/images/<?php echo $toppic ?>" alt="page1" id="mainPageImg" />
That's all. Phew. Finally got it to work :) Had to do it for a client, too!

The slug for your Articles page has to be defined as articles. This is set in the edit page interface, see these directions.

Related

How to get parent page id and title of current page in wordpress?

Here is the my code, which is not working.
global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );
but this is not working.
thank you in advance.
For parent page id
$post->post_parent;
For current page title
$post->post_title;
For parent page id
<?php
echo wp_get_post_parent_id(get_the_ID());
?>
In Gutenberg:
wp.data.select('core/editor').getEditedPostAttribute('parent')
Hope will be helpful to someone
If you want i.e: create a link to the post parent:
<a href="<?= get_permalink($post->post_parent) ?>">
<?= get_the_title($post->post_parent) ?>
</a>
→ <?= the_title() ?>
which will result in i.e:
Latest News → Some news title
For Astra theme and for page template look.php I did this:
$post->post_parent; will nbot work cause in my case function is out of the loop. I run it via functions.php. $post->post_parent works perfectly when inserting it in page template, but not when editing theme function ;)
function add_script_before_header() {
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
echo get_the_title($grandparent);
}
$after = $parent;
if ( is_page_template( 'look.php' ) ) {
echo $after . ' - ';
}
}

hide a div in specific custom taxonomy category

I was working on site pages here which comes in wedding category which comes in a custom taxonomy named listing.In my wedding category listing i dont want to display the price sticker but it shoud display in all other listing posts eg this page
I have tried
if ( in_category( 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
also
if ( is_category( 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
also
if ( has_term('listing', 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
none of these worked.Please help.Thank You
Please try this once where the div "pricestricker" is present:
<div class="pricestricker" <?php if ( in_category( 'wedding' )) { echo 'style="display:none;"' }?>><span itemprop="price" class="wlt_shortcode_price">$0</span></div>
Here you should only write the php code inside the div "pricestricker" and remain will be default code.
or you can do:
<?php if ( !in_category( 'wedding' )) { ?>
<div class="pricestricker"><span itemprop="price" class="wlt_shortcode_price">$0</span></div> // This will be your default code
<?php } ?>

Medium size image by plugin called dynamic featured image

i am using dynamic featured image and adding multiple product images to a single custom post type name as products for a single product but and i am trying to get those images in my template but the array only return me only two sizes [thumb] [full] but i need medium as well below is my code
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
As you guys can see in the anchor tag $featured_image['medium'] this is how i want to echo this anchor tag but unfortunately it don't return me the medium size and i need help in getting the medium size as well. below is the array that i get where you can clearly see only [thumb] and [full]. please help
Array
(
[thumb] => http://www.example.com/wp-content/uploads/2014/07/product-1-120x90.jpg
[full] => http://www.example.com/wp-content/uploads/2014/07/product-1.jpg
[attachment_id] => 254
)
You need to get medium sized image by calling get_image_url function. Try this:
<?php
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images();
foreach($featured_images as $featured_image) {
$mediumSizedImage = $dynamic_featured_image->get_image_url($featured_image['attachment_id'], 'medium');
echo "<img src = '" . $mediumSizedImage . "' />";
?>
<img width="60" src="<?php echo $featured_image['full'];?>"/>
<?php }
}
?>
All available functions are documented here.
PS: I am author of the plugin.

how to hide featured image from SOME posts

Is there a way to hide the featured image from SOME posts?
My blog is cur-mudg-eon.com and if you look at the most recent post (on the homepage) titled "Confucius Says..." you'll see that I've used the featured image and it shows some excerpt text. When you click on the title or pic it takes you to the post which shows the cartoon I wish to display, and the featured image that I want to hide/remove.
I only want to do this on some posts, but I would like to be able to keep the featured image on the homepage.
Is this possible?
EDIT:
Pastebin File as requested.
Based on Chris Herberts answer where would I add his code to this code found in my single.php file:
<?php if(has_post_thumbnail()) {
echo '<figure class="featured-thumbnail"><span class="img-wrap">'; the_post_thumbnail(); echo '</span></figure>';
}
?>
<?php } else { ?>
<?php if(has_post_thumbnail()) {
echo '<figure class="featured-thumbnail large"><span class="img-wrap"><span class="f-thumb-wrap">'; the_post_thumbnail('post-thumbnail-xl'); echo '</span></span></figure>';
}
?>
<?php } ?>
Another way to do this that does not depend on them all being in the same category is to use a Custom Field.
You would set a custom field for the post for which you would hide the featured image - in the image below I'm using "hide_featured_image" and "yes", as the key and value, respectively.
Then you would check to see if the "hide_featured_image" field is set to "yes" when calling calling the function to show the featured image. Here's an example:
$shouldHideFeaturedImage = get_post_meta($post->ID, 'hide_featured_image', true);
if ( $shouldHideFeaturedImage != 'yes' ) {
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
}
If all those posts are in the same categories, you can do something like this.
On your theme files, under the file single.php there should be something similar to this:
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
change it to something like:
if ( !in_category( array( 'category1', 'category2', 'etc' ) )) {
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
}

timthumb NOT_FOUND_IMAGE resize

In a wordpress site, i have defined a default NOT_FOUND_IMAGE in timthumb config file:
// "http://example.com/timthumb-config.php".
<?php
if(! defined('NOT_FOUND_IMAGE') ) define ('NOT_FOUND_IMAGE', 'http://example.com/img/default.jpg');
Is there any way to force resizing of this image depending on timthumb request parameters. For instance:
// 404 occurs
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2F404-image.jpg&h=180&w=120
// get resized (cropped) default image
timthumb.php?src=http:%2F%2Fexample.com%2Fimg%2Fdefault.jpg&h=180&w=120
Try this code:
if (!defined('NOT_FOUND_IMAGE'))
define ('NOT_FOUND_IMAGE','images/ingredient.jpg');
I don't rate timbthumb, and I think it has security issues. Install and use the ThumbGen plugin instead.
You can then use it really easily with a normal editable conditional php statement.. (Pseudo)
<?php if you have a thumbnail {
Output the img with thumbgen resized
} else {
Output the no image, you can even use thumbgen if you want
}
?>
Real world example:
<?php if ( has_post_thumbnail() )
{
$image_id = get_post_thumbnail_id();
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
?>
<img src='<?php thumbGen($image_url,175,175, "crop=1"); ?>' />
<?php
}
else
{
?>
<img src="<?php bloginfo('template_url');?>/images/no-photo.png" alt="No Photo!">
<?php
}
?>

Resources