在 wordpress 網站上安裝 ACF 插件并配置一些自定義欄位后,我想僅在我的帖子(文章)中顯示 ACF 自定義欄位。我按照指南進行操作,但在不使用子主題的情況下找不到如何操作。即使主題將被更新而不會丟失我的代碼,我也需要創建一個執行操作。我想要一個簡單的插件,可以將下面的代碼添加到我的帖子中。我對php還不是很熟悉。這是我在單個模板中插入的代碼:
<div id="scheda-dettagli-vino" class="dettagli-vino">
<table>
<tr>
<td>
<?php if (get_field('produttore'))
{ echo '<img src="/downloads/area-di-produzione.png" />' , '<h3 style="display:inline; ">Area di produzione</h3>';}?>
</td>
<td>
<?php if(get_field('produttore'))
{ echo the_field('produttore');}?>
</td>
</tr>
</table>
</div>
uj5u.com熱心網友回復:
首先,您需要在插件目錄中創建一個檔案夾,并創建一個擴展名必須為檔案夾名稱.php 的檔案。
我分享一個示例代碼,您只需將其粘貼即可。激活插件后。希望,作業。
示例檔案夾名稱:acf-helper
示例插件:acf-helper.php
<?php
/**
* Plugin Name: ACF-Helper
* Plugin URI: #
* Description: Custom plugin for ACF.
* Author: Monzur Alam
* Version: 1.0.0
* Text Domain: acf-helper
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
if (!function_exists('acf_helper_content')) {
function acf_helper_content( $content ){
if(is_singular('post')){
ob_start();
?>
<div id="scheda-dettagli-vino" class="dettagli-vino">
<table>
<tr>
<td>
<?php
if (get_field('produttore')) {
echo '<img src="/downloads/area-di-produzione.png" />', '<h3 style="display:inline; ">Area di produzione</h3>';
}
?>
</td>
<td>
<?php
if (get_field('produttore')) {
echo esc_html(the_field('produttore'));
}
?>
</td>
</tr>
</table>
</div>
<?php
return $content . ob_get_clean();
}
}
add_filter('the_content', 'acf_helper_content');
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/414433.html
標籤:
