0

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).

  • body_class will let you target a post in different contexts – Milo Dec 28 '18 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 Dec 28 '18 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 Dec 28 '18 at 14:08
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.