タイトルに記載されているように、(他のプラグインとの互換性のため)主にACFカスタムフィールドに基づいてpost_excerptとpost_thumbnailの値を自動的に保存/更新します。これを実現するためにトリングながら今、私が最初に次の関数であること、2つの問題が発生しました:Wordpressの抜粋を設定し、カスタムフィールドに基づいてサムネイルを投稿
仕事、しかし、時にはそれが私が持っていることを意味、私は保存するだけで二時間を(値を更新しているようだんfunction test_FeaturedImageSetByACF() {
$current_screen = get_current_screen(); // Current admin screen needed to identify the current cpt
$current_cpt_name = $current_screen->post_type; // Current cpt name
$current_cpt_support = 'thumbnail'; // We want to check if the CPT supports this feature
$post_id = get_the_ID(); // Current post ID
$post_image_field = get_field('post_head_img'); // ACF field we want to sync
$post_image_id = $post_image_field['id']; // ACF image filed ID
$post_image_url = $post_image_field['url']; // ACF image filed URL
// If current cpt supports thumbnails/featured images
if (post_type_supports($current_cpt_name, $current_cpt_support)) {
if (($post_image_url) AND (($post_image_url) != (get_the_post_thumbnail()))) {
delete_post_thumbnail($post_id);
set_post_thumbnail($post_id, $post_image_id);
}
}
}
add_action('save_post', 'test_FeaturedImageSetByACF', 13, 2);
add_action('publish_post', 'test_FeaturedImageSetByACF', 10, 2);
2回保存する)。私は間違ったフックや間違った優先順位などを使用していることを理解していますが、どれがどれであるかわかりません。
私が持っている第2の問題は、抜粋の抜粋についても同様のことを達成したいのですか?今では関数は以前のものと全く同じように見えますが、更新する値がわかりません。誰かが私を正しい方向に向けることができますか?事前に
おかげ
:(削除される可能性があります投稿したり、何かのイベントで)これは私が抜粋部分とカスタムポストサムネイル/おすすめ画像の両方を使用したコードがある一方、興味がある人のために、トピックはhttps://support.advancedcustomfields.com/forums/topic/set-wordpress-excerpt-and-post-thumbnail-based-on-custom-field/で見つけることができますそれは動作しますが、同じ方法で "save_post"フックで行います。イメージを変更し、ポストを保存して何も起こりません(古いフィーチャイメージです)。ページをリフレッシュまたは開くと何も起こりません。私は2回目を保存し、それは最終的に変更され、カスタムフィールドと投稿サムネイルは同じ画像を表示します。 – Cerere