重写函数,使其不使用echo php与短代码一起使用
我在获取短代码以将函数的html放在wordpress上的正确位置时遇到了一些问题。经过一些研究,我发现当使用短代码时,函数中不能使用重写函数,使其不使用echo php与短代码一起使用,php,wordpress,Php,Wordpress,我在获取短代码以将函数的html放在wordpress上的正确位置时遇到了一些问题。经过一些研究,我发现当使用短代码时,函数中不能使用echo。我一直在尽最大努力编写没有任何echo的函数,但似乎无法让它工作。我对PHP作为一种语言几乎没有实际经验,我正在尽我最大的努力 上面的代码块是整个过程中使用了echo的运行代码,第二个代码块是没有任何echo但不运行的重写代码。有人知道我的重写哪里出错了吗?或者可能有更好的解决方法,然后重写函数 第一: function na_get_gallery_i
echo
。我一直在尽最大努力编写没有任何echo
的函数,但似乎无法让它工作。我对PHP作为一种语言几乎没有实际经验,我正在尽我最大的努力
上面的代码块是整个过程中使用了echo
的运行代码,第二个代码块是没有任何echo
但不运行的重写代码。有人知道我的重写哪里出错了吗?或者可能有更好的解决方法,然后重写函数
第一:
function na_get_gallery_image_urls( $post_id ) {
$post = get_post($post_id);
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return;
//initiate counter to be able to keep track of images
$count_img = 0;
// Retrieve all galleries of this post
$gallery = get_post_gallery_images( $post );
// Loop through each image in first gallery
foreach( $gallery as $image ) {
// Below here are the main containers and first large image; stuff we will only want to output one time.
if($count_img == 0) { ?>
<!-- Whole Gallery container (inludes thumbnails) -->
<div id="instant-gallery">
<!-- Main Display Area -->
<div id="ig-main">
<!-- Set the parameters for the image we are about to display. -->
<?php
$default_attr = "ig-hero";
?>
<!-- Display the first image attachment as the large image in the main gallery area -->
<img src="<?php print $image; ?>" id="<?php print $default_attr; ?>">
<a href="<?php print $image; ?>" download>Download</a>
<!-- Close the main display area -->
</div>
<!-- Open the Thumbnail navigation -->
<ul id="ig-thumbs">
<!-- End the block of stuff that we only do for the first image -->
<?php } ?>
<!-- Now, for each of the thumbnail images, label the LI with an ID of the appropriate thumbnail number -->
<li id="ig-thumb-<?php print $count_img+1; ?>">
<?php if ($count_img==0) {
// If this is the first thumbnail, add a class of 'selected' to it so it will be highlighted
$thumb_attr = "thumb selected";
} else {
// For all other thumbnails, just add the basic class of 'thumb'
$thumb_attr = "thumb";
} ?>
<!-- Output a thumbnail-sized version of the image that has the attributes defined above -->
<img src="<?php print $image; ?>" class="<?php print $thumb_attr; ?>">
</li>
<!-- Increment the counter so we can keep track of which thumbnail we are on -->
<?php $count_img = $count_img + 1; } ?>
<!-- Close the thumbnail navigation list -->
</ul>
<!-- Close the entire Gallery -->
</div>
<?php
}
function instant_gallery() {
global $post;
$args = array(
'post_parent' => '$post->ID', // For the current post
'post_type' => 'attachment', // Get all post attachments
'post_mime_type' => 'image', // Only grab images
'order' => 'ASC', // List in ascending order
'orderby' => 'menu_order', // List them in their menu order
'numberposts' => -1, // Show all attachments
'post_status' => null, // For any post status
);
// Retrieve the items that match our query; in this case, images attached to the current post.
na_get_gallery_image_urls($args[0]);
}
// Create the Shortcode
add_shortcode('instant_gallery', 'instant_gallery');
?>
函数na\u get\u gallery\u image\u url($post\u id){
$post=get\u post($post\u id);
//确保帖子中有图库
如果(!有_快捷码($post->post_内容,'gallery'))
返回;
//启动计数器以跟踪图像
$count\u img=0;
//检索此帖子的所有图库
$gallery=获取\u post\u gallery\u图像($post);
//循环浏览第一个库中的每个图像
foreach($gallery作为$image){
//下面是主容器和第一个大图像;我们只想输出一次。
如果($count\u img==0){?>
“id=”“>
“类别=”“>
最后:
函数na\u get\u gallery\u image\u url($post\u id){
$post=get\u post($post\u id);
//确保帖子中有图库
如果(!有_快捷码($post->post_内容,'gallery'))
返回;
//启动计数器以跟踪图像
$count\u img=0;
//检索此帖子的所有图库
$gallery=获取\u post\u gallery\u图像($post);
//循环浏览第一个库中的每个图像
foreach($gallery作为$image){
//下面是主容器和第一个大图像;我们只想输出一次。
如果($count_img==0){
//
$hero_img='';
//
$hero_img.='';
//
$default\u attr=“ig hero”;
//
$hero_img.='';
$hero_img.='';
//关闭主显示区
$hero_img.='';
//
$hero_img.='';
//
}
//
$thumbs='- ”;
如果($count\u img==0){
//如果这是第一个缩略图,请向其中添加一个“选定”类,以便突出显示
$thumb\u attr=“thumb selected”;
}否则{
//对于所有其他缩略图,只需添加基本类“thumb”
$thumb\u attr=“thumb”;
}
//
$thumbs.=''。
';
//
$count\u img=$count\u img+1;
}
//
$fin=“
”;
//
$fin.='';
$content=“$hero_img”。$thumbs”。$fin”;
返回$content;
}
函数即时_gallery(){
全球$员额;
$args=数组(
'post_parent'=>'$post->ID',//用于当前帖子
'post_type'=>'attachment',//获取所有post附件
'post_mime_type'=>'image',//仅抓取图像
'order'=>'ASC',//按升序列出
'orderby'=>'menu_order',//按菜单顺序列出它们
'numberposts'=>-1,//显示所有附件
'post_status'=>null,//对于任何post状态
);
//检索与我们的查询匹配的项目;在本例中,是附加到当前帖子的图像。
na_get_gallery_image_URL($post->ID);
}
//创建短代码
添加快捷码(“即时画廊”、“即时画廊”);
?>
您好,请在下面的代码中尝试更改您的参数,它将起作用
function testgallery_shortcode( $atts , $content = null ) {
extract( shortcode_atts(
array(
'title' =>'TEST GALLERY',
'items' =>3,
), $atts )
);
$args = array(
'post_type' => 'testgallery',
'posts_per_page' => $items,
'post_status' => 'publish',
);
$test_gallery = new WP_Query( $args );
$html = '';
global $post;
if ( $test_gallery->have_posts() ) :while ( $test_gallery->have_posts() ) : $test_gallery->the_post();
$html.='<div class="col-sm-4 col-xs-12">';
$html.='<figure class="post">';
$html.='<div class="image-wrap">';
$html.='<a class="zoomtoplay" alt="gallery_image" href="'. esc_url( get_permalink() ).'"></a>';
if (has_post_thumbnail() ) {
$imgarray = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full');
$imgURL = $imgarray[0];
$html.='<img src="'.$imgURL.'" class="img-responsive"/>';
}
$html.='<a href="'.esc_url( get_permalink() ).'"></div><h3>'.get_the_title().'</h3></a>';
$html.='</figure>';
$html.='</div>';
endwhile;
endif;
wp_reset_query();
return $html;
}
add_shortcode( 'testgallery', 'testgallery_shortcode' );
function testgallery\u短代码($atts,$content=null){
提取(短码)(
排列(
“标题”=>“测试库”,
“项目”=>3,
),$atts)
);
$args=数组(
“post_type”=>“testgallery”,
“每页发布”=>$items,
“发布状态”=>“发布”,
);
$test\u gallery=新的WP\u查询($args);
$html='';
全球$员额;
如果($test_gallery->have_posts()):而($test_gallery->have_posts()):$test_gallery->the_post();
$html.='';
$html.='';
$html.='';
$html.='';
如果(具有\u post\u缩略图()){
$imgaray=wp\u get\u attachment\u image\u src(get\u post\u缩略图\u id($post->id),'full');
$imgURL=$imgarray[0];
$html.='';
}
$html.='';
$html.='';
$html.='';
结束时;
endif;
wp_reset_query();
返回$html;
}
添加_短代码('testgallery','testgallery_短代码');
--现在,您可以在文章和页面中使用[testgallery]短代码
这是创建短代码的标准方法
如果需要更多详细信息,您仍然可以访问此链接-谢谢!!在我以前的函数中,我使用的是
get\u post\u gallery\u images()
我是否仍然使用该功能以及合并此功能?不,您现在不需要使用该功能,您可以从这里直接传递您的帖子结构,我在示例中已完成此操作再次感谢@ManthanDave!我将输入我的参数/结构,并在得到答案后标记该问题,因此我无法将此解决方案发送到work co正确。所以我决定坚持我最了解的,用CSS改变页面布局,使我的图库在主容器id上有一个值位置:绝对值,然后将各种元素的边距弄乱,直到页面看起来很好。顶部图库就是实现的。再次感谢@Manthan帮助我@TimothyGriffith别担心继续帮助:)
function testgallery_shortcode( $atts , $content = null ) {
extract( shortcode_atts(
array(
'title' =>'TEST GALLERY',
'items' =>3,
), $atts )
);
$args = array(
'post_type' => 'testgallery',
'posts_per_page' => $items,
'post_status' => 'publish',
);
$test_gallery = new WP_Query( $args );
$html = '';
global $post;
if ( $test_gallery->have_posts() ) :while ( $test_gallery->have_posts() ) : $test_gallery->the_post();
$html.='<div class="col-sm-4 col-xs-12">';
$html.='<figure class="post">';
$html.='<div class="image-wrap">';
$html.='<a class="zoomtoplay" alt="gallery_image" href="'. esc_url( get_permalink() ).'"></a>';
if (has_post_thumbnail() ) {
$imgarray = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full');
$imgURL = $imgarray[0];
$html.='<img src="'.$imgURL.'" class="img-responsive"/>';
}
$html.='<a href="'.esc_url( get_permalink() ).'"></div><h3>'.get_the_title().'</h3></a>';
$html.='</figure>';
$html.='</div>';
endwhile;
endif;
wp_reset_query();
return $html;
}
add_shortcode( 'testgallery', 'testgallery_shortcode' );