$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>add different custom fields value to post class if permalink or index|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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

Post a comment

comment list (0)

  1. No comments so far