add different custom fields value to post class if permalink or index

admin2025-06-05  3

As the title suggests, I am trying to apply the values from different custom fields to the posts classes depending on wether the post is viewed in the index page or permalink page. I am not a coder, so I have limited understanding of what I should be doing.

I am aware of the following functions

( get_option('permalink_structure') ) & (get_permalink) & (get_the_permalink)

but I am unsure on where to go next with these? At the moment my content.pho has the following code in it

<article id="post-<?php the_ID(); ?>" <?php post_class(get_field('custom_field_X')); ?>

Which alters the style of the post when seen in the index page to a style dependant on the value inside custom_field_x.

What I need to happen is -

a> for that style to only apply if the post is viewed in the index page,

b> and to stop applying when vieiwng the post as a permalink, and for a different value, say from custom_field_Y, to apply instead

c> this permalink custom field, custom_field_Y, also needs to only apply when in a permalink page and stop applying if in index or any other page.

I guess I would like the code to check for index or permalink first and then apply the values of the appropriate custom field following the succesful fulfilment of a check, or otherwise not apply anything.

the goal is to have a particular style for the individual post in the index page (which has been achieved), and then to have a particular style of the same post when viewed in permalink page (currently stuck on).

As the title suggests, I am trying to apply the values from different custom fields to the posts classes depending on wether the post is viewed in the index page or permalink page. I am not a coder, so I have limited understanding of what I should be doing.

I am aware of the following functions

( get_option('permalink_structure') ) & (get_permalink) & (get_the_permalink)

but I am unsure on where to go next with these? At the moment my content.pho has the following code in it

<article id="post-<?php the_ID(); ?>" <?php post_class(get_field('custom_field_X')); ?>

Which alters the style of the post when seen in the index page to a style dependant on the value inside custom_field_x.

What I need to happen is -

a> for that style to only apply if the post is viewed in the index page,

b> and to stop applying when vieiwng the post as a permalink, and for a different value, say from custom_field_Y, to apply instead

c> this permalink custom field, custom_field_Y, also needs to only apply when in a permalink page and stop applying if in index or any other page.

I guess I would like the code to check for index or permalink first and then apply the values of the appropriate custom field following the succesful fulfilment of a check, or otherwise not apply anything.

the goal is to have a particular style for the individual post in the index page (which has been achieved), and then to have a particular style of the same post when viewed in permalink page (currently stuck on).

Share Improve this question asked Dec 28, 2018 at 4:39 bldingbloksbldingbloks 33 bronze badges 3
  • body_class will let you target a post in different contexts – Milo Commented Dec 28, 2018 at 4:54
  • From what I've read in the codex that seems like it could be relevant but I dont know where to begin when it comes to writing the php to make use of it, do you have any examples of that achieve what I'm looking for? – bldingbloks Commented Dec 28, 2018 at 5:09
  • You don’t need any php, it’s just css classes- .single .your-class targets the post on a single view, .blog .your-class targets your post in the index view. – Milo Commented Dec 28, 2018 at 14:08
Add a comment  | 

1 Answer 1

Reset to default 0

ok have a working fix, I used an if else statement combined with is_home to change

<article id="post-<?php the_ID(); ?>" <?php post_class(get_field('custom_field_X')); ?>

to

<?php if ( is_home()) post_class(get_field('custom_field_X')); 
else post_class(get_field('custom_field_Y')); ?>>

which has removed the custom_field_x settings from the permalink pages fo the posts.

maybe there is a more elegant solution out there but from my current knowledge this is the best I could do

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749063097a316037.html

最新回复(0)