In Wordpress.. I'm looking for a way to add the first uploaded image in a post automatically to a custom field named "Image".
Does anyone know this?
I found the answer my self
Here is the final code that adds the thumbnail url to a custom field named Image.
function w_thumbnail_src() {
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
return $thumb[0]; // thumbnail url
} else {
return ''; // or a default thumbnail url
}
}
add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}
Related
I am trying to have the first img from post to be the featured img automaticlly
This is what I added in my fuctions.php in theme editor
// Auto add featured image
function wpsites_auto_set_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {set_post_thumbnail($post->ID, $attachment_id);}
}
}
}
add_action('the_post', 'wpsites_auto_set_featured_image');
Is there something I need to add or change I am realtivly new to php
I’ve created a field group called ‘main_image’ and under it I have 2 fields
1.
‘main_image_logged’
type: image
2.
‘main_image_logout’
type:image
what im trying to do is to show the classic featured image which comes with the post for all, and for users that is logged in show the image in field “main_image_logged’
for the logout i even tried to set name:_thumbnail_id which took the image from ‘main_image_logout’ and used it as a featured image.
is there any way how to do this?
if user is logged out -> featured image from field ‘main_image_logout’
if user is logged in -> featured image from field ‘main_image_logged’
tried something like this but it's wrong
function acf_set_featured_image( $value, $post_id, $field ){
if (is_user_logged_in()) {
if($value != ''){
//Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta($post_id, '_thumbnail_id', $value);
}
return $value;
}
}
add_filter('acf/update_value/name=main_image_logged', 'acf_set_featured_image', 10, 3);
using:
Wordpress
Advanced custom fields
Divi theme
thanks alot guys
Try this
add_filter('post_thumbnail_id', 'replace_thumbnail_id_with_acf', 20, 2);
function replace_thumbnail_id_with_acf($thumbnail_id, $post) {
if ( is_user_logged_in() ) {
$image_id = get_field('reveal_face', $post->ID, false);
if ($image_id) {
$thumbnail_id = $image_id;
}
} else {
$image_id = get_field('_thumbnail_id', $post->ID, false);
if ($image_id) {
$thumbnail_id = $image_id;
}
}
return $thumbnail_id;
}
I want to customize my archive-list on my WordPress page so it displays something like this:
POST TITLE - 2 days ago
POST TITLE - 4 days ago
etc...
So far I only managed to display the post title with following code:
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 16) ); ?>
I have no idea how to move forward from this, any help?
The function wp_get_archives calls the function get_archives_link to prepare the output. The last step done in that function is to apply the filter by the same name as the function (i.e., get_archives_link). So to modify as you desire, define your own filter function and add that filter in your functions.php file.
For example, the following code would add a class to the output of the function get_archives_link.
function example_get_archives_link($link_html) {
if (is_day() || is_month() || is_year()) {
if (is_day()) {
$data = get_the_time('Y/m/d');
} elseif (is_month()) {
$data = get_the_time('Y/m');
} elseif (is_year()) {
$data = get_the_time('Y');
}
// Link to archive page
$link = home_url($data);
// Check if the link is in string
$strpos = strpos($link_html, $link);
// Add class if link has been found
if ($strpos !== false) {
$link_html = str_replace('<li>', '<li class="current-archive">', $link_html);
}
}
return $link_html;
}
add_filter("get_archives_link", "example_get_archives_link");
You can find more info about the filter in the Codex.
To set a different header per page in WordPress, I usually edit php get_header(); line in theme files such as index.php, page.php...
I wonder if it's possible to change the header file per page with 'get_header' action without editing theme files such as page.php.
I tried the following code, but it didn't work.
function themeslug_header_hook( $name ) {
if(is_front_page() || is_home()) {
$name = 'home';
}
$return $name;
}
add_action( 'get_header', 'themeslug_header_hook' );
Is there any way to set a different header per page within the functions.php file?
Thanks.
I assume you could always do something like this in your functions file.
function get_my_header() {
if( !is_home() ) {
global $post;
// get category by ID
$category = get_the_category($post->ID);
// first category slug
$catslug = $category[0]->slug;
// is there is a category slug call the header-$catslug.php
if (isset($catslug)) {
get_header($catslug);
} else {
// else call normal header.php
get_header();
}// ends !is_home()
// else call normal header
} else {
get_header();
}// ends isset()
} // ends get_myheader function
After searching (so ?s=foobar), I change the query :
function SearchFilter($query)
{
// If 's' request variable is set but empty
if(isset($_GET['s'])
&& empty($_GET['s'])
&& $query->is_main_query())
{
$query->is_search = true;
$query->is_home = false;
}
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('post','myFooBarCustomType'));
}
return $query;
}
But after the query, wordpress routes to the search.php page and I want to route to the single-customType.php page with the only-one result of my query. (a direct to the answer route with the good URL like www.mywebsite/myCustomType/foobar)
I only want the search.php page for a null result or more than 1.
Please help
Instead of your code, try adding this piece of code to your functions.php:
add_action('template_redirect', 'search_redirect_to_first_result');
function search_redirect_to_first_result() {
if (is_search()) {
if (have_posts()) {
the_post();
wp_redirect(get_permalink());
exit;
}
}
}