admin管理员组

文章数量:1415664

I used this code to display and make shortcode of custom post type. its worked, i wnat to remove the anchor tag on this , when i remove anchor tag its shows error hoe can i fix this ? here code :

<?php
/*
Plugin Name: Custom Post Type - Portfolio Items
Plugin URI: 
Description: Custom Post Types and shortcodes for Portfolio Items.
Version: 1.0
Author: Michael W. Delaney
Author URI:
*/
add_shortcode( 'portfolio', 'portfolio_shortcode' );
function add_menu_icons_styles_portfolio(){
?>

<style>
#adminmenu .menu-icon-portfolio div.wp-menu-image:before {
    content: "\f322";
}
</style>

<?php
}
add_action( 'admin_head', 'add_menu_icons_styles_portfolio' );
add_action( 'init', 'portfolio_custom_init' );
function portfolio_custom_init() {
  $labels = array(
    'name' => _x('Portfolio', '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'),
    'all_items' => __('All Portfolio Items'),
    'view_item' => __('View Portfolio Items'),
    'search_items' => __('Search Portfolio Items'),
    'not_found' =>  __('No portfolio items found'),
    'not_found_in_trash' => __('No potfolio items found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Portfolio'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array( 'title', 'editor', 'revisions', 'page-attributes', 'thumbnail', 'excerpt' ),
    'menu_icon' => '',
  ); 
  register_post_type('portfolio',$args);
}
// Add Shortcode
  function portfolio_shortcode( $atts ) {

    extract( shortcode_atts( array(
      "name" => false,
    ), $atts ) );

    global $cfs;
    $ul_class = 'list-portfolio';
    $ul_class .= ( $name )     ? '' : ' row';

    $li_class = 'list-portfolio-item';
    $col_class = ( $name )     ? '' : ' col-sm-6';

    $lis = '';
    if($name) {
        $query = new WP_Query( array(
            'post_type' => 'portfolio',
            'name' => $name,
            'order' => 'ASC',
        ) );
    } else {
    $query = new WP_Query( array(
        'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
    ) );
    }
   while ( $query->have_posts() ) : $query->the_post();
        $lis .= sprintf(
            '<div class="%s"><a class="%s" href="%s"> %s <h4>%s</h4><p>%s</p></a></div>',
              esc_attr( $col_class ),
              esc_attr( $li_class ),
              esc_url( get_permalink() ),
              get_the_post_thumbnail( $post_id, 'thumbnail', array('class' => 'pull-left portfolio-thumb img-circle') ),
              get_the_title(),
              get_the_excerpt()
        );


    endwhile;
    wp_reset_query();  
    return sprintf( 
          '<div class="%s">%s</div>',
          esc_attr( $ul_class ),
          $lis
        );
}
?>

I used this code to display and make shortcode of custom post type. its worked, i wnat to remove the anchor tag on this , when i remove anchor tag its shows error hoe can i fix this ? here code :

<?php
/*
Plugin Name: Custom Post Type - Portfolio Items
Plugin URI: 
Description: Custom Post Types and shortcodes for Portfolio Items.
Version: 1.0
Author: Michael W. Delaney
Author URI:
*/
add_shortcode( 'portfolio', 'portfolio_shortcode' );
function add_menu_icons_styles_portfolio(){
?>

<style>
#adminmenu .menu-icon-portfolio div.wp-menu-image:before {
    content: "\f322";
}
</style>

<?php
}
add_action( 'admin_head', 'add_menu_icons_styles_portfolio' );
add_action( 'init', 'portfolio_custom_init' );
function portfolio_custom_init() {
  $labels = array(
    'name' => _x('Portfolio', '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'),
    'all_items' => __('All Portfolio Items'),
    'view_item' => __('View Portfolio Items'),
    'search_items' => __('Search Portfolio Items'),
    'not_found' =>  __('No portfolio items found'),
    'not_found_in_trash' => __('No potfolio items found in Trash'), 
    'parent_item_colon' => '',
    'menu_name' => 'Portfolio'
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array( 'title', 'editor', 'revisions', 'page-attributes', 'thumbnail', 'excerpt' ),
    'menu_icon' => '',
  ); 
  register_post_type('portfolio',$args);
}
// Add Shortcode
  function portfolio_shortcode( $atts ) {

    extract( shortcode_atts( array(
      "name" => false,
    ), $atts ) );

    global $cfs;
    $ul_class = 'list-portfolio';
    $ul_class .= ( $name )     ? '' : ' row';

    $li_class = 'list-portfolio-item';
    $col_class = ( $name )     ? '' : ' col-sm-6';

    $lis = '';
    if($name) {
        $query = new WP_Query( array(
            'post_type' => 'portfolio',
            'name' => $name,
            'order' => 'ASC',
        ) );
    } else {
    $query = new WP_Query( array(
        'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
    ) );
    }
   while ( $query->have_posts() ) : $query->the_post();
        $lis .= sprintf(
            '<div class="%s"><a class="%s" href="%s"> %s <h4>%s</h4><p>%s</p></a></div>',
              esc_attr( $col_class ),
              esc_attr( $li_class ),
              esc_url( get_permalink() ),
              get_the_post_thumbnail( $post_id, 'thumbnail', array('class' => 'pull-left portfolio-thumb img-circle') ),
              get_the_title(),
              get_the_excerpt()
        );


    endwhile;
    wp_reset_query();  
    return sprintf( 
          '<div class="%s">%s</div>',
          esc_attr( $ul_class ),
          $lis
        );
}
?>
Share Improve this question edited Aug 26, 2019 at 14:30 zain_ali 663 bronze badges asked Aug 26, 2019 at 10:25 rubyruby 35 bronze badges 1
  • Since you're using sprintf() you'll have to remove not only the anchor tag itself but also the two search strings - i.e. esc_attr($li_class) and esc_url(get_permalink()). – WebElaine Commented Aug 26, 2019 at 14:45
Add a comment  | 

1 Answer 1

Reset to default 1

Here is the right way of removing the anchor tag.

$lis .= sprintf( '<div class="%s"> %s <h4>%s</h4><p>%s</p></div>', esc_attr( $col_class ), get_the_post_thumbnail( $post_id, 'thumbnail', array('class' => 'pull-left portfolio-thumb img-circle') ), get_the_title(), get_the_excerpt() );

本文标签: WordPress Custom Post Type and Shortcode for Portfolio Items