admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 0ok 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
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_classwill 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-classtargets the post on a single view,.blog .your-classtargets your post in the index view. – Milo Commented Dec 28, 2018 at 14:08
1 Answer
Reset to default 0ok 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
本文标签: add different custom fields value to post class if permalink or index
版权声明:本文标题:add different custom fields value to post class if permalink or index 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749063072a2310398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


body_classwill let you target a post in different contexts – Milo Commented Dec 28, 2018 at 4:54.single .your-classtargets the post on a single view,.blog .your-classtargets your post in the index view. – Milo Commented Dec 28, 2018 at 14:08