一直在学习如何在没有插件的情况下手动添加自定义帖子类型和自定义字段,方法是从这里和那里提取代码片段并将它们添加到 functions.php 中。这是我将自定义字段添加到自定义帖子类型的方法
function add_ads_meta_boxes() {
add_meta_box("ads_contact_meta", "URL", "add_custom_ads_meta_box", "ads", "normal", "low");
}
function add_custom_ads_meta_box()
{
global $post;
$custom = get_post_custom( $post->ID );
?>
<style>.width99 {width:99%;}</style>
<p>
<input type="text" name="link-url" value="<?= @$custom["link-url"][0] ?>" class="width99" />
</p>
<?php
}
function save_ads_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "link-url", @$_POST["link-url"]);
}
}
add_action( 'admin_init', 'add_ads_meta_boxes' );
add_action( 'save_post', 'save_ads_custom_fields' );
我可以看到帖子类型中的字段,它保存了添加的信息。现在,这是我将它添加到网站的方式
<div class="customadwrap">
<?php $args = array( 'post_type' => 'ads');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php echo get_post_meta($post->ID, 'link-url', true); ?>">
<?php the_title(); ?>
</a>
<?php
endwhile;
?>
</div>
标题显示没有问题,但输入的网址没有问题。我如何使 fref 工作?
在您的第二段代码中,替换$post->ID
为get_the_ID()
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句