Skip Navigation

[Closed] Custom field code is not working on "Posts Page"

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by Minesh 4 years ago.

Assisted by: Minesh.

Author
Posts
#1800543

Hello, I am using the code below to add a class to my navigation determined by a custom field. This works fine in all pages but does not work for the page designated the "Posts Page" (index.php). The code is placed in header.php. Please let me know why it is not working or how to make this work?

Thank you!

<?php
$nav_style = get_post_meta($post->ID, 'wpcf-dimes-header-style', true); // Find header style
if ($nav_style == '1') {
$nav_class = 'beige-bg';
} elseif ($nav_style == '2') {
$nav_class = 'transparent';
} elseif ($nav_style == '3') {
$nav_class = 'mud-bg';
}
?>
<?php echo $nav_class; ?>

#1801375

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The blog page lists posts, it is not itself a post, and there is therefore no post meta to retrieve for it.

You probably want to update your code to hard-code one of the class options for the blog page, and then use the is_home() function in your conditions to check whether you are on the blog page or not, see https://developer.wordpress.org/themes/basics/conditional-tags/#the-main-page

#1801377

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I see you are using custom code where you are using some PHP code to get the custom field value.

Do you see you get any value from the custom field where you use the variable $nav_style?

$nav_style = get_post_meta($post->ID, 'wpcf-dimes-header-style', true); // Find header style

If no - what if you try to define the following line at top of your code:

 global $post;
#1804685

I don't see any result on the posts page from the variable $nav_style.

I tried adding global $post; to my code and this didn't work unfortunately. This works fine on all single pages and single posts but not on the "Posts Page". Any other ideas?


<?php
global $post;
	$nav_style = get_post_meta($post->ID, 'wpcf-dimes-header-style', true); // Find header style
	if ($nav_style == '1') { 
		$nav_class = 'beige-bg';
	} elseif ($nav_style == '2') { 
		$nav_class = 'transparent';
	} elseif ($nav_style == '3') { 
		$nav_class = 'mud-bg';
	} 
?>
<?php echo $nav_class; ?> 
#1804745

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is strange.

Can you please share problem URL as well as access details with information about where exactly you added the code you mention.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

The topic ‘[Closed] Custom field code is not working on "Posts Page"’ is closed to new replies.