WordPress:使用短代码加载特定页面的功能
WordPress: Function for loading a specific page with shortcode
我在 WordPress 中有这样的页面结构:
Materials //parent
-> Plastic //children
-> Metal //children
-> ...
我想在我需要的地方包含我的文件 grid-fasads.php
中的内容和简码。
我在 product.php 中使用了简码:
$fasads = get_field('fasad_type');
foreach ($fasads as $f) {
echo do_shortcode('[grid-fasad]');
}
这个函数我写在functions.php:
function get_fasads($atts, $content = null ) {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array( 'post_type' => 'page' ) );
// get a specific page ID
$materials = get_page( 4702 );
// filter for choosing a children pages of 'materials'
$materials_children = get_page_children( $materials->ID, $all_wp_pages );
foreach ( $materials_children as $children ) {
$page = $children->ID;
echo $page;
}
// It's ok. The pages IDs are shown.
// After that I have this:
$recent = new WP_Query( "page_id = " . $page );
while( $recent->have_posts() ) : $recent->the_post();
require( '/fasads/grid-fasads.php' );
the_content();
endwhile;
}
add_shortcode( 'grid-fasad','get_fasads' );
我明白问题是( "page_id = " . $page)
。我需要为 'Materials' 页面的每个子页面包含一个 .php 文件的内容。试图将最后一部分代码插入循环,但每次都失败。
这是来自
的片段
我也试过这个循环:
foreach ($materials_children as $children) {
$page = $children->ID;
$recent = new WP_Query("page_id=".$page);
while ($recent->have_posts()) : $recent->the_post();
require('/fasads/grid-fasads.php');
the_content();
endwhile;
print_r(array_count_values((array)$page));
}
但结果它显示了重复的值。出了点问题。
拜托,帮忙。
我已经解决了这个问题。
这是一个函数:
function get_pallette() {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
// get a specific page ID
$materials = get_page_by_title('Materials');
$materials_children = get_page_children( $materials->ID, $all_wp_pages );
if (isset ($materials_children)) :
foreach ($materials_children as $children) {
$page = $children->ID;
}
$fasads = get_field('fasad_type');
if (isset ($fasads)) :
foreach ($fasads as $f) {
$p = get_page_by_title( $f );
if (!empty ($p)) :
echo do_shortcode("[grid-fasad page=\"$p->ID\"]");
else :
//do something;
endif;
}
else :
//do something;;
endif;
endif;
//do something;
}
这是另一个生成简码的函数:
function get_fasads($atts, $content = null ){
$atts = shortcode_atts(
array(
'page' => 0
), $atts, 'grid-fasad');
$recent = new WP_Query('page_id='. $atts['page']);
$path = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/theme/fasads/grid-fasads.php';
while ($recent->have_posts()) : $recent->the_post();
require $path;
endwhile;
}
add_shortcode('grid-fasad','get_fasads');
一个商品的页面片段:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); global $_product; $_product = new jigoshop_product( $post->ID ); ?>
<div id="post-<?php $id_page ?>" <?php post_class(); ?>>
<div class="summary">
<?php do_action('jigoshop_after_single_product_summary', $post, $_product); ?>
...
<?php get_pallette(); ?>
</div>
<?php endwhile; ?>
也许这不是一个优雅的决定,但它确实有效。
仍然只有 1 个问题:执行函数 get_pallette() 后,页面失去了它自己的 ID,因为我使用 "new WP_Query" 和页面子项的新 ID。如果此功能在操作之前执行 "jigoshop_after_single_product_summary"(如我所愿),它无法加载实际商品页面的内容。
我在 WordPress 中有这样的页面结构:
Materials //parent
-> Plastic //children
-> Metal //children
-> ...
我想在我需要的地方包含我的文件 grid-fasads.php
中的内容和简码。
我在 product.php 中使用了简码:
$fasads = get_field('fasad_type');
foreach ($fasads as $f) {
echo do_shortcode('[grid-fasad]');
}
这个函数我写在functions.php:
function get_fasads($atts, $content = null ) {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array( 'post_type' => 'page' ) );
// get a specific page ID
$materials = get_page( 4702 );
// filter for choosing a children pages of 'materials'
$materials_children = get_page_children( $materials->ID, $all_wp_pages );
foreach ( $materials_children as $children ) {
$page = $children->ID;
echo $page;
}
// It's ok. The pages IDs are shown.
// After that I have this:
$recent = new WP_Query( "page_id = " . $page );
while( $recent->have_posts() ) : $recent->the_post();
require( '/fasads/grid-fasads.php' );
the_content();
endwhile;
}
add_shortcode( 'grid-fasad','get_fasads' );
我明白问题是( "page_id = " . $page)
。我需要为 'Materials' 页面的每个子页面包含一个 .php 文件的内容。试图将最后一部分代码插入循环,但每次都失败。
这是来自
的片段我也试过这个循环:
foreach ($materials_children as $children) {
$page = $children->ID;
$recent = new WP_Query("page_id=".$page);
while ($recent->have_posts()) : $recent->the_post();
require('/fasads/grid-fasads.php');
the_content();
endwhile;
print_r(array_count_values((array)$page));
}
但结果它显示了重复的值。出了点问题。
拜托,帮忙。
我已经解决了这个问题。
这是一个函数:
function get_pallette() {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
// get a specific page ID
$materials = get_page_by_title('Materials');
$materials_children = get_page_children( $materials->ID, $all_wp_pages );
if (isset ($materials_children)) :
foreach ($materials_children as $children) {
$page = $children->ID;
}
$fasads = get_field('fasad_type');
if (isset ($fasads)) :
foreach ($fasads as $f) {
$p = get_page_by_title( $f );
if (!empty ($p)) :
echo do_shortcode("[grid-fasad page=\"$p->ID\"]");
else :
//do something;
endif;
}
else :
//do something;;
endif;
endif;
//do something;
}
这是另一个生成简码的函数:
function get_fasads($atts, $content = null ){
$atts = shortcode_atts(
array(
'page' => 0
), $atts, 'grid-fasad');
$recent = new WP_Query('page_id='. $atts['page']);
$path = $_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/theme/fasads/grid-fasads.php';
while ($recent->have_posts()) : $recent->the_post();
require $path;
endwhile;
}
add_shortcode('grid-fasad','get_fasads');
一个商品的页面片段:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); global $_product; $_product = new jigoshop_product( $post->ID ); ?>
<div id="post-<?php $id_page ?>" <?php post_class(); ?>>
<div class="summary">
<?php do_action('jigoshop_after_single_product_summary', $post, $_product); ?>
...
<?php get_pallette(); ?>
</div>
<?php endwhile; ?>
也许这不是一个优雅的决定,但它确实有效。 仍然只有 1 个问题:执行函数 get_pallette() 后,页面失去了它自己的 ID,因为我使用 "new WP_Query" 和页面子项的新 ID。如果此功能在操作之前执行 "jigoshop_after_single_product_summary"(如我所愿),它无法加载实际商品页面的内容。