在短码参数中传递证明海关帖子的potid

时间:2014-07-17 作者:Payal

我正在尝试使用快捷码创建推荐自定义帖子和显示

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。我正在尝试这样做,但它没有显示任何输出请告诉我哪里我错了你的建议帮助我很多。

4 个回复
SO网友:HU ist Sebastian

嗯,也许您不应该设置$args[\'posts id\',因为这不是get\\u posts函数支持的参数,而是使用“posts\\u in”参数,或者如果您获得id参数,则切换到“get\\u post”函数。此外,我不太确定,但我认为您必须在短代码中使用引号。

因此,您的短代码函数应该如下所示:

function load_testimonials($a){
     $thePostID = $post->ID;
    $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(isset( $a[\'id\'])){
     echo   $args[\'posts__in\'] = array((int)$a[\'id\']);
    }
    //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>\';
}
然后这样称呼它:

<?php echo do_shortcode(\'[testimonials id="3751"]\'); ?>
快乐的编码,

Kuchenundkakao

SO网友:Abhik

试试这个。。这是一个非常基本的代码,您可能需要对其进行修改以使其完全工作,但这将帮助您开始。

function load_testimonials( $atts ) {


    extract( shortcode_atts(
        array(
            \'id\' => \'\',
        ), $atts )
    );

    $out = \'<div id="testimonials" class="flexslider">\';
        $out .= \'<ul class="slides">\';

        $post = get_post( $id );
        if ( $post) {
        $url_thumb = wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID));
        $link = get_url($post);
            $out .= \'<li>\';
                $out .= \'<div class="slide-testimonials">\';
                    ....
                    ....
                $out .= \'</div>\';
            $out .= \'</li>\';
        }
        $out .= \'</ul>\';
    $out .= \'</div>\';


    return $out;
}
add_shortcode( \'testimonials\', \'load_testimonials\' );

SO网友:Aamer Shahzad

如果您使用get_the_ID() 然后它会得到ID 当短代码被放置在Post、page或自定义Post类型的循环内时,在循环外我们有一个global variable $post 其中包含post object, 获取ID 在您可以使用的循环之外$post->ID

在shorcode中,我建议您使用$post->ID 如果短代码在循环中,它将获得该帖子的id,否则它将获得ID 第一个帖子的。

SO网友:Sabbir Hasan

只需用给定的代码替换您的短代码函数。那就照常说吧。它现在应该可以很好地工作了。

//registering the shortcode to show testimonials
    function load_testimonials($a){
    $atts = shortcode_atts( array(
        \'rand\' => \'\',
        \'max\' => \'\',
        \'id\' => \'\',
    ), $a);

$args = array(
    "post_type" => "testimonials",
);

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");

结束

相关推荐

Basic do shortcodes question

我使用WordPress codex页面进行编码。正如您所看到的,我有两个PHP代码。页面能够获得代码的值,但它不遵守CSS规则,我相信这是因为我以错误的方式插入了PHP代码。您能看一下下面的代码并提出问题所在吗?<?php echo do_shortcode(\'[groups_non_member group=\"VPS\"]\' .\"<p class=\'vod-time-price\'>\" . the_field(\'run_t\') . \"MIN | $\". the