自定义帖子类型Shortode将位于其他页面内容之上。有没有窃听器?

时间:2013-06-24 作者:Hooman Askari

我已经为我的wordpress主题创建了一个自定义的公文包帖子类型,我也为它做了一个快捷代码。但是,当我创建一个页面并使用短代码来显示有限数量的公文包项目时,无论我在页面的何处插入我的短代码,公文包项目总是位于其他内容的顶部,我查看了页面的源代码,没有理由在页面上处理任何其他代码之前先处理此短代码。

我甚至在一篇文章上进行了测试,当文章在主博客存档页面上查看时,会在我想要的地方处理短代码,但当文章再次被视为一篇文章时,短代码会被处理并显示在该文章或页面的所有内容之上。

以下是我用于短代码的代码:

    add_shortcode( \'portfolios\', \'ub_portfolios_sc\' );
    function ub_portfolios_sc ( $atts ) {
    global $post;
    $default = array(
        \'type\'         => \'post\',
        \'post_type\'    => \'portfolios\',
        \'limit\'        => 12,
        \'order\'        => \'ASC\',
        \'orderby\'      => \'date\',
        \'status\'       => \'publish\',
        \'post_columns\' => \'three\'
    );
    $r = shortcode_atts( $default, $atts );
    extract( $r );

    if( empty($post_type) )
    $post_type = $type;

    $post_type_ob = get_post_type_object( $post_type );
    if( !$post_type_ob )
    return \'<div class="warning"><p>No such post type <em>\' . $post_type . \'</em> found.</p></div>\';

    $args = array(
        \'post_type\'    => $post_type,
        \'numberposts\'  => $limit,
        \'post_status\'  => $status,
        \'post_columns\' => $post_columns,
        \'order\'        => $order,
        \'orderby\'      => $orderby
    );
    $posts = get_posts( $args );
    if( count($posts) ):
        echo \'<ul id="portfolio-grid" class="portfolio-grid unstyled \'.$post_columns.\'-cols">\';
        foreach( $posts as $post ) : setup_postdata( $post );
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" data-largesrc="<?php if ( has_post_thumbnail() ) { echo ub_featured_img_url(\'portfolio-fullsize\'); } else { ?>http://placehold.it/600&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); } ?>" data-title="<?php the_title() ?>" data-button="<?php _e(\'View Project\', \'une_boutique\'); ?>" data-description="<?php the_excerpt() ?>">
                <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { ?>
                <img src="http://placehold.it/320&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); ?>" alt="no thumbnails">
                <?php } ?>
            <span class="item-title"><span class="arrow-shape-up"></span><?php the_title() ?></span>
            </a>
        </li>
        <?php
        endforeach; wp_reset_postdata();
        echo \'</ul>\';
    endif;
}
首先,我是如何创建自定义帖子类型的:

add_action(\'init\', \'portfolio_register\', 0);

function portfolio_register() {

$labels = array(
    \'name\' => _x(\'Portfolios\', \'post type general name\'),
    \'singular_name\' => _x(\'Portfolio Item\', \'post type singular name\'),
    \'add_new\' => _x(\'Add New\', \'portfolio item\'),
    \'add_new_item\' => __(\'Add New Portfolio Item\'),
    \'edit_item\' => __(\'Edit Portfolio Item\'),
    \'new_item\' => __(\'New Portfolio Item\'),
    \'view_item\' => __(\'View Portfolio Item\'),
    \'search_items\' => __(\'Search Portfolio\'),
    \'not_found\' =>  __(\'Nothing found\'),
    \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
    \'parent_item_colon\' => \'\'
);

$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'menu_icon\' => get_template_directory_uri() . \'/images/sofa.png\',
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'menu_position\' => null,
    \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'excerpt\'),
    \'taxonomies\' => array(\'post_tag\')
  ); 

register_post_type( \'portfolios\' , $args );
flush_rewrite_rules();

set_post_thumbnail_size( 600, 600, true );
}

register_taxonomy("Categories",
array("portfolios"),
    array(
        "hierarchical" => true,
        "label" => __(\'Categories\', \'une_boutique\'),
        "singular_label" => __(\'Category\', \'une_boutique\'),
        "rewrite" => true
    )
);

add_action("admin_init", "admin_init", 10);

function admin_init(){
add_meta_box("project_date-meta", __(\'Project Date\', \'une_boutique\'), "project_date", "portfolios", "side", "low");
add_meta_box("project_client-meta", __(\'Project Client\', \'une_boutique\'), "project_client", "portfolios", "side", "low");
add_meta_box("project_link-meta", __(\'Project Link\', \'une_boutique\'), "project_link", "portfolios", "side", "low");
}

function project_date(){
global $post;
$custom = get_post_custom($post->ID);
$project_date = $custom["project_date"][0];
?>
<label for="project-date"><?php __(\'Year:\', \'une_boutique\') ?></label><br />
<input id="project-date" name="project_date" value="<?php echo $project_date; ?>" size="42" />
<?php
}

function project_client(){
global $post;
$custom = get_post_custom($post->ID);
$project_client = $custom["project_client"][0];
?>
<label for="project_client"><?php __(\'Project Client:\', \'une_boutique\') ?></label><br />
<input id="project_client" name="project_client" value="<?php echo $project_client; ?>" size="42" />
<?php
 }

function project_link(){
global $post;
$custom = get_post_custom($post->ID);
$project_link = $custom["project_link"][0];
?>
<label for="project_link"><?php __(\'Project Link:\', \'une_boutique\') ?></label><br />
<input id="project_link" name="project_link" value="<?php echo $project_link; ?>" size="42" />
<?php
}

add_action(\'save_post\', \'save_portfolio_details\');
function save_portfolio_details() {
global $post;

update_post_meta($post->ID, "project_date", $_POST["project_date"]);
update_post_meta($post->ID, "project_client", $_POST["project_client"]);
update_post_meta($post->ID, "project_link", $_POST["project_link"]);
 }

add_action("manage_posts_custom_column",  "portfolios_custom_columns");
add_filter("manage_edit-portfolios_columns", "portfolios_edit_columns");

function portfolios_edit_columns($columns){
$columns = array(
    "cb" => "<input type=\\"checkbox\\" />",
    "title" => __(\'Item Title\', \'une_boutique\'),
    "year" => __(\'Project Date\', \'une_boutique\'),
    "Categories" => __(\'Categories\', \'une_boutique\'),
    "thumbnail" => __(\'Item Preview\', \'une_boutique\'),
);

return $columns;
 }

// GET FEATURED IMAGE for it\'s column
function ub_get_featured_image($post_ID) {
$post_thumbnail_id = get_post_thumbnail_id($post_ID);
if ($post_thumbnail_id) {
    $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, \'thumbnail\');
    return $post_thumbnail_img[0];
}
}

function portfolios_custom_columns($column){
  global $post;

  switch ($column) {
case "year":
    $custom = get_post_custom();
    echo $custom["project_date"][0];
break;
case "Categories":
    echo get_the_term_list($post->ID, \'Categories\', \'\', \', \',\'\');
break;
case "thumbnail":
    global $post_ID;
    $post_featured_image = ub_get_featured_image($post_ID);
    if ($post_featured_image) {
        echo \'<img src="\' . $post_featured_image . \'" width="50" height="50" />\';
    }
break;
  }
}
谁知道我做错了哪一部分?

1 个回复
最合适的回答,由SO网友:Mike Madern 整理而成

您的shortcode函数中有以下代码:

if( count($posts) ):
        echo \'<ul id="portfolio-grid" class="portfolio-grid unstyled \'.$post_columns.\'-cols">\';
        foreach( $posts as $post ) : setup_postdata( $post );
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" data-largesrc="<?php if ( has_post_thumbnail() ) { echo ub_featured_img_url(\'portfolio-fullsize\'); } else { ?>http://placehold.it/600&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); } ?>" data-title="<?php the_title() ?>" data-button="<?php _e(\'View Project\', \'une_boutique\'); ?>" data-description="<?php the_excerpt() ?>">
                <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { ?>
                <img src="http://placehold.it/320&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); ?>" alt="no thumbnails">
                <?php } ?>
            <span class="item-title"><span class="arrow-shape-up"></span><?php the_title() ?></span>
            </a>
        </li>
        <?php
        endforeach; wp_reset_postdata();
        echo \'</ul>\';
    endif;
短代码应return 他们的内容
将此部分更改为以下内容:

if( count($posts) ):
  ob_start();

  echo \'<ul id="portfolio-grid" class="portfolio-grid unstyled \'.$post_columns.\'-cols">\';
        foreach( $posts as $post ) : setup_postdata( $post );
        ?>
        <li>
            <a href="<?php the_permalink(); ?>" data-largesrc="<?php if ( has_post_thumbnail() ) { echo ub_featured_img_url(\'portfolio-fullsize\'); } else { ?>http://placehold.it/600&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); } ?>" data-title="<?php the_title() ?>" data-button="<?php _e(\'View Project\', \'une_boutique\'); ?>" data-description="<?php the_excerpt() ?>">
                <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { ?>
                <img src="http://placehold.it/320&text=<?php _e(\'No Thumbnails\', \'une_boutique\'); ?>" alt="no thumbnails">
                <?php } ?>
            <span class="item-title"><span class="arrow-shape-up"></span><?php the_title() ?></span>
            </a>
        </li>
        <?php
        endforeach; wp_reset_postdata();
        echo \'</ul>\';

  return ob_get_clean();
endif;

结束

相关推荐

Shortcode of a function

我得到了这个函数,我想将“foreach”的contents divs作为return。我该怎么做?<?php $args = array( \'numberposts\' => 6, \'post_status\'=>\"publish\",\'post_type\'=>\"post\",\'orderby\'=>\"post_date\"); $postslist = get_posts( $args ); fo