我正在尝试使用快捷码创建推荐自定义帖子和显示
function create_post_type() {
register_post_type(
\'testimonials\',//new post type
array(
\'labels\' => array(
\'name\' => __( \'Testimonials\' ),
\'singular_name\' => __( \'Testimonial\' )
),
\'public\' => true,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'custom_fields\'),
\'hierarchical\' => false
)
);
}
add_action( \'init\', \'create_post_type\' );
//adding the URL meta box field
function add_custom_metabox() {
add_meta_box( \'custom-metabox\', __( \'Link\' ), \'url_custom_metabox\', \'testimonials\', \'side\', \'low\' );
}
add_action( \'admin_init\', \'add_custom_metabox\' );
// HTML for the admin area
function url_custom_metabox() {
global $post;
$urllink = get_post_meta( $post->ID, \'urllink\', true );
//validating!
if ( ! preg_match( "/http(s?):\\/\\//", $urllink ) && $urllink != "") {
$errors = "This URL isn\'t valid";
$urllink = "http://";
}
// output invlid url message and add the http:// to the input field
if( isset($errors) ) { echo $errors; }
?>
<p>
<label for="siteurl">URL:<br />
<input id="siteurl" size="37" name="siteurl" value="<?php if( isset($urllink) ) { echo $urllink; } ?>" />
</label>
</p>
<?php
}
//saves custom field data
function save_custom_url( $post_id ) {
global $post;
if( isset($_POST[\'siteurl\']) ) {
update_post_meta( $post->ID, \'urllink\', $_POST[\'siteurl\'] );
}
}
add_action( \'save_post\', \'save_custom_url\' );
//return URL for a post
function get_url($post) {
$urllink = get_post_meta( $post->ID, \'urllink\', true );
return $urllink;
}
//registering the shortcode to show testimonials
function load_testimonials($a){
$args = array(
"post_type" => "testimonials",
"id" => $post->ID
);
if( isset( $a[\'rand\'] ) && $a[\'rand\'] == true ) {
$args[\'orderby\'] = \'rand\';
}
if( isset( $a[\'max\'] ) ) {
$args[\'posts_per_page\'] =(int) $a[\'max\'];
}
if( $a[\'id\'] ) {
$posts_in = array_map( \'intval\', explode( \',\',$a[\'id\'] ) );
$args[\'post__in\'] = $posts_in;
}
//getting all testimonials
$posts = get_posts($args);
echo \'<div id="testimonials" class="flexslider">\';
echo \'<ul class="slides">\';
foreach($posts as $post)
{
$url_thumb = wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID));
$link = get_url($post);
echo \'<li>\';
echo \'<div class="slide-testimonials">\';
if ( ! empty( $url_thumb ) ) { echo \'<img src="\'.$url_thumb.\'" />\'; }
if ( ! empty( $post->post_content ) ) { echo \'<p>\'.$post->post_content.\'<br />\'; }
echo \'<h2>\'.$post->post_title.\'</h2>\';
if ( ! empty( $link ) ) { echo \'<a href="\'.$link.\'">Visit Site</a></p>\'; }
echo \'</div>\';
echo \'</li>\';
}
echo \'</ul>\';
echo \'</div>\';
}
add_shortcode("testimonials","load_testimonials");
add_filter(\'widget_text\', \'do_shortcode\');
我使用的显示快捷码是
<?php echo do_shortcode(\'[testimonials id=3751]\'); ?>
我想在短代码中传递帖子id,并在我的页面中显示特定的帖子id。我正在尝试这样做,但它没有显示任何输出请告诉我哪里我错了你的建议帮助我很多。