How to condition this a WP Custom field if exist - wordpress

In some Wordpress Posts I want to show the featured thumbnail image shown:
fearless_post_thumbnail(); //located in content.php of template
But in some posts I want to hide that thumbnail, In that case I would like to create a custom
field (hide_thumb = 1 )
in posts where I want to hide it:
Question: Which condition should I wrap around:
fearless_post_thumbnail(); // it shows thumbnail.
Thank you

not sure I understand your question, but it sounds like you want to do this:
if ( $hide_thumb != 1 ) {
fearless_post_thumbnail();
}
If your custom field, $hide_thumb, is set to TRUE or 1, then your thumbnail function will not execute.

The other answer here assumed you had already retrieved the value of of your custom field. As per http://codex.wordpress.org/Function_Reference/get_post_custom,
$custom_fields = get_post_custom();
$hide_thumb = $custom_fields['hide_thumb'];
if ( $hide_thumb[0] != 1) {
fearless_post_thumbnail();
}

Related

How to hide a content on WordPress after receiving a certain number of views

How can I hide content in a post or page after it has received a certain amount of views I have set using a shortcode in WordPress?
Let's say I make a post. I enclose some content in the shortcode. I set the content to be shown for just 500 views. Then once the post reaches 500 views, the content should disappear from the post or page.
I have tried so many other plugins but couldn't find any solutions to this. wp-limit-post-views plugin also didn't solve my problem. I need help on this.
You could try something like that:
function hide_contents_function($atts, $content) {
$attributes = shortcode_atts(
array(
'count' => 500
),
$atts
);
// Get the max counts for the current post from the DB.
// You could use either an options if the counter is global, or the post meta.
// For this example I am using options, but it's up to you the implementation
$total_count = get_option('total_count', 0);
// Alternative way using post meta to get the counter per page/post
$total_count = get_post_meta(get_the_ID(), 'post_view_count', true);
if ( ! $total_count ) {
$total_count = 0;
}
// If the count loaded from the DB is bigger than the count
// property value then return nothing.
if ( $total_count > (int)$attributes['count'] ) {
return '';
}
return do_shortcode($content);
}
add_shortcode('hide_contents', 'hide_contents_function');
The above code, will register a short code that accepts an attribute allowing you to control how many views you want to have before you hide the contents.
In this example I used a single value from options table, but you are free to use any method you like to count the total views of a single post.
To use this short code you can do something like that:
[hide_contents count="345"]I will be hidden after 345 views.[/hide_contents]
Note that if you have installed any cache system, your content will not be hidden if the page is cached! That's not a problem of the short code, but the problem will occur because of the cache.
Finally, remember to update the counter of the views on each post refresh :)

ACF default field group settings for "hide on screen"

I would like to set default settings for style and hide on screen settings within the field group settings.
I've found posts talking about modifying settings for individual ACF fields like the WYSIWYG or image fields like so.
add_filter( 'acf/get_valid_field', 'change_post_content_type');
function change_post_content_type( $field ) {
if($field['type'] == 'wysiwyg') {
$field['tabs'] = 'visual';
$field['media_upload'] = 0;
}
if($field['type'] == 'image') {
$field['preview_size'] = 'small';
}
if($field['type'] == 'style') {
$field['style'] = 'seamless';
}
return $field;
}
Using this overrides whatever is selected rather than setting the value as default but it's good enough for what I need.
The image and WYSIWG field work fine but I can't get it working on the field group setting fields. I don't think the $field['type'] == 'style' is correct but as it doesn't follow the same structure as the other fields I don't know what I should be using.
Any ideas?
update
I've found this but I can't figure out how to use it. The following doesn't work
add_action('acf/render_field_group_settings', 'change_field_group_settings', 10, 1);
function change_field_group_settings( $field_group ) {
$field_group['style'] = 'seemless';
$field_group['hide_on_screen'] = array('the_content');
return $field_group;
}
You may need change your syntax to use proper formatting for style and hide_on_screen, for example:
'hide_on_screen' => array(
0 => 'the_content',
),
Best practice would be to create a new Field Group in your WP CMS, publish it, and navigate to:
Custom Fields > Tools > (check the box for your Field Group) > Generate PHP
That way you can view the proper output.

Make Custom Post Type with custom field inherit category's custom field when empty

been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?

how to add fields from referenced node drupal 8

I have a content type that contains a field that is a reference to another node. I'm trying to include a field from the referenced node within the page for the main node, but I can't figure out how to add that. Here's how I'm getting the value within my theme:
function mytheme_preprocess_node(array &$variables) {
$node = $variables['node'];
if ( $node->get('field_testimonial') ) {
$referenced_nodes = $node->get('field_testimonial')->referencedEntities();
if ( count($referenced_nodes) > 0 ){
$referenced_node = $referenced_nodes[0];
//this is providing the value I want. how can I add that back to my page?
error_log($referenced_node->body->value);
}
}
}
Please help me add that value back to my variables so I can use it in my theme! Thank you for your help.
Just do the following
$variables['referenced_body'] = $referenced_node->body->value;
In your Twig-Template you can do this:
{{ referenced_body }}
Why are you doing with code ? it can configure form admin
Go to admin/structure/types/manage/article/display and manage your desire format to display the reference node.
Thanks

Change Wordpress feed <link> for one specific tag

I have an external page that reads a RSS feed for a tag. I can't change anything on the external page, so the challenge is to change the RSS feed on my side to match the requirements.
On the external page the 3 latest posts for a tag are shown, and at the end of the section (note: not after each post but after all 3 posts) there is a "View all" link. This link receives its value from the element in the feed, which is by default set to my blog homepage, e.g. http://myblog.com). For this specific tag the link should be http://myblog.com/tag/myspecialtag.
The requirement is that the "View all" link links to the tag page instead of the homepage.
My idea was to add a condition to the element to change the URL for this specific category. I tried to change the feed template as recommended here: Customizing feeds, but for some reason it doesn't change the template at all. The code I tried is the following:
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'change_feed_rss2', 10, 1 );
function change_feed_rss2( $for_comments ) {
$rss_template = get_template_directory() . '/feeds/feed-custom_rss2.php';
if( file_exists( $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2( $for_comments ); // Call default function
}
Of course I created the custom feed template and stored it in the feeds directory in my theme.
As this didn't work I tried looking into filters/hooks but I couldn't find anything helpful regarding my issue. Any ideas on how to best solve this issue?
I came up with the following solution:
I created a new custom page template custom_rss-feed.php and copied the code from wp-includes/feed-rss.php into it. I assigned this template to a new page. Additionally I added a filter to get_bloginfo_rss like the following:
function alter_bloginfo_rss($value, $key) {
if ( $key === "url" &&
is_page_template("custom_rss-feed.php")
) {
return $value . "/my/custom/url";
}
return $value;
}
add_filter("get_bloginfo_rss", "alter_bloginfo_rss", 10, 2);

Resources