I have a text file with post ids and attachment ids like so:
post_id,attachment_id
post_id,attachment_id
post_id,attachment_id
post_id,attachment_id
I then try to use a function inside my functions.php file like so:
function addimg() {
$fh = @fopen( dirname( __FILE__ ) . '/addimg.txt', 'r' );
if ( $fh ) {
while ( ( $line = fgets( $fh ) ) !== false ) {
$ids = explode( ',', $line );
array_walk( $ids, 'trim' );
$postid = array_shift( $ids );
set_post_thumbnail( $postid, $ids );
}
}
}
add_action('wp_head', 'addimg');
But nothing happens.
I've used the same procedure to add categories to posts and it works. I've also tried to manually add a featured image to a post like so:
function thumb() {
set_post_thumbnail( 1, 1);
}
add_action('wp_head', 'thumb');
And that works.
So my knowledge is too shallow to understand why what I'm trying to do doesn't work?
while
-loop)? Are lines actually being read? By the way, usingarray_walk
this way generates a notice, if I remember correctly, because of the number of arguments.array_map
would be suited better.functions.php
without the need of an external text file? Like create some array withpost_id => attachment_id
and then for each pairset_post_thumbnail
?