通过将 args 值传递到 url 将 3 个 Wordpress 模板合并为一个
Combine 3 Wordpress Templates into one by passing the args value into the url
我编写了一个存档模板,可以将自定义 post 类型的艺术家输出到页面中。我不得不引入一个过滤器来将艺术家组织成订单。三个订单分别是 All('orderby' => 'rand') Newest('orderby'=> 'post_date') 和 ALPHABETICAL('orderby' => 'title') .我采用的方法是我创建了 3 个模板并相应地更改了 orderby 参数,这为三个不同的过滤器类别制作了 3 个不同的模板。每个模板的完整代码如下,唯一的例外是 orderby,它会根据模板进行更改。
<?php
/*
Template Name: Aritsts
*/
$args = array(
'category' => $id,
'post_type' => 'artists',
'post_status' => 'publish',
'orderby' => 'rand',
'order' => 'ASC',
);
get_header(); ?>
<div id="content" style="width:100%">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-content">
<article id="content-items">
<div id="filter-artist">
<span>FILTER:</span>
<?php
echo '<a href="'.get_site_url().'/artists" class="active-filter">ALL</a>';
echo '<a href="'.get_site_url().'/artists-newest" >NEWEST</a>';
echo '<a href="'.get_site_url().'/artists-alphabetical" >ALPHABETICAL</a>';
?>
</div>
<div id="load-items">
<?php
$query = new WP_Query($args );
if ( $query -> have_posts()) {
while ($query->have_posts())
{
$query->the_post();
$post_title = get_the_title( get_the_ID() );
$post_facebook_url = get_post_meta( get_the_ID(), 'artist_facebook_url', true );
$post_twitter_url = get_post_meta( get_the_ID(), 'artist_twitter_url', true);
$post_instagram_url = get_post_meta( get_the_ID(), 'artist_instagram_url', true);
$post_website_url = get_post_meta( get_the_ID(), 'artist_website_url', true);
$post_thumbnail = get_the_post_thumbnail( $post_id, 'medium' );
$post_default = get_site_url()
?>
<div class="artist">
<div class="thumb-artist">
<?php
if(!$post_thumbnail)
{
echo '<img width="165" height="110" src="'.get_template_directory_uri().'/images/no-photo.jpg" class="attachment-220x110 wp-post-image" />';
}
else
{
echo $post_thumbnail ;
}
?>
<span class="social-content">
<a href= <?php if(!$post_facebook_url){echo site_url();} else{echo $post_facebook_url;}?> target="_blank" <?php if(!$post_facebook_url){echo 'style="display:none;"';} else{echo 'class="facebook social"';} ?> > </a>
<a href= <?php if(!$post_twitter_url){echo site_url();} else{echo $post_twitter_url;}?> target="_blank" <?php if(!$post_twitter_url){echo 'style="display:none;"';} else{echo 'class="twitter social"';} ?> > </a>
<a href= <?php if(!$post_instagram_url){echo site_url();} else{echo $post_instagram_url;}?> target="_blank" <?php if(!$post_instagram_url){echo 'style="display:none;"';} else{echo 'class="instagram social"';} ?> > </a>
<a href= <?php if(!$post_website_url){echo site_url();} else{echo $post_website_url;}?> target="_blank" <?php if(!$post_website_url){echo 'style="display:none;"';} else{echo 'class="web social sat-color"';} ?> > </a>
</span>
</div>
<h3 class="name-artist"><?php echo $post_title; ?></h3>
</div>
<?php } } else { echo wpautop( 'Sorry, no posts were found' ); } ?>
</div></article></div></div>
<?php get_footer(); ?>
我想要实现的是通过创建三个不同的参数将这 3 个模板合并为一个模板,并将它们传递到 url 并在此处更新 url 值:
<?php
echo '<a href="'.get_site_url().'/artists">ALL</a>';
echo '<a href="'.get_site_url().'/artists-newest" >NEWEST</a>';
echo '<a href="'.get_site_url().'/artists-alphabetical" class="active-filter">ALPHABETICAL</a>';
?>
我是 php 的菜鸟所以请不要评判我 :(
您可以只使用一个模板来处理不同的查询,这里有一个解决方案:
将您的查询更改为此
$args = array(
'category' => $id,
'post_type' => 'artists',
'post_status' => 'publish',
'orderby' => ($_GET["orderby"]),
'order' => ($_GET["order"]),
);
然后您可以在您的链接中使用 URLs,例如:
<a href="'.get_site_url().'/artists-alphabetical?orderby=title&order=ASC" >ALPHABETICAL</a>
$_Get
从 URL 接收一个变量并将其传递到您的查询中。
我编写了一个存档模板,可以将自定义 post 类型的艺术家输出到页面中。我不得不引入一个过滤器来将艺术家组织成订单。三个订单分别是 All('orderby' => 'rand') Newest('orderby'=> 'post_date') 和 ALPHABETICAL('orderby' => 'title') .我采用的方法是我创建了 3 个模板并相应地更改了 orderby 参数,这为三个不同的过滤器类别制作了 3 个不同的模板。每个模板的完整代码如下,唯一的例外是 orderby,它会根据模板进行更改。
<?php
/*
Template Name: Aritsts
*/
$args = array(
'category' => $id,
'post_type' => 'artists',
'post_status' => 'publish',
'orderby' => 'rand',
'order' => 'ASC',
);
get_header(); ?>
<div id="content" style="width:100%">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-content">
<article id="content-items">
<div id="filter-artist">
<span>FILTER:</span>
<?php
echo '<a href="'.get_site_url().'/artists" class="active-filter">ALL</a>';
echo '<a href="'.get_site_url().'/artists-newest" >NEWEST</a>';
echo '<a href="'.get_site_url().'/artists-alphabetical" >ALPHABETICAL</a>';
?>
</div>
<div id="load-items">
<?php
$query = new WP_Query($args );
if ( $query -> have_posts()) {
while ($query->have_posts())
{
$query->the_post();
$post_title = get_the_title( get_the_ID() );
$post_facebook_url = get_post_meta( get_the_ID(), 'artist_facebook_url', true );
$post_twitter_url = get_post_meta( get_the_ID(), 'artist_twitter_url', true);
$post_instagram_url = get_post_meta( get_the_ID(), 'artist_instagram_url', true);
$post_website_url = get_post_meta( get_the_ID(), 'artist_website_url', true);
$post_thumbnail = get_the_post_thumbnail( $post_id, 'medium' );
$post_default = get_site_url()
?>
<div class="artist">
<div class="thumb-artist">
<?php
if(!$post_thumbnail)
{
echo '<img width="165" height="110" src="'.get_template_directory_uri().'/images/no-photo.jpg" class="attachment-220x110 wp-post-image" />';
}
else
{
echo $post_thumbnail ;
}
?>
<span class="social-content">
<a href= <?php if(!$post_facebook_url){echo site_url();} else{echo $post_facebook_url;}?> target="_blank" <?php if(!$post_facebook_url){echo 'style="display:none;"';} else{echo 'class="facebook social"';} ?> > </a>
<a href= <?php if(!$post_twitter_url){echo site_url();} else{echo $post_twitter_url;}?> target="_blank" <?php if(!$post_twitter_url){echo 'style="display:none;"';} else{echo 'class="twitter social"';} ?> > </a>
<a href= <?php if(!$post_instagram_url){echo site_url();} else{echo $post_instagram_url;}?> target="_blank" <?php if(!$post_instagram_url){echo 'style="display:none;"';} else{echo 'class="instagram social"';} ?> > </a>
<a href= <?php if(!$post_website_url){echo site_url();} else{echo $post_website_url;}?> target="_blank" <?php if(!$post_website_url){echo 'style="display:none;"';} else{echo 'class="web social sat-color"';} ?> > </a>
</span>
</div>
<h3 class="name-artist"><?php echo $post_title; ?></h3>
</div>
<?php } } else { echo wpautop( 'Sorry, no posts were found' ); } ?>
</div></article></div></div>
<?php get_footer(); ?>
我想要实现的是通过创建三个不同的参数将这 3 个模板合并为一个模板,并将它们传递到 url 并在此处更新 url 值:
<?php
echo '<a href="'.get_site_url().'/artists">ALL</a>';
echo '<a href="'.get_site_url().'/artists-newest" >NEWEST</a>';
echo '<a href="'.get_site_url().'/artists-alphabetical" class="active-filter">ALPHABETICAL</a>';
?>
我是 php 的菜鸟所以请不要评判我 :(
您可以只使用一个模板来处理不同的查询,这里有一个解决方案:
将您的查询更改为此
$args = array(
'category' => $id,
'post_type' => 'artists',
'post_status' => 'publish',
'orderby' => ($_GET["orderby"]),
'order' => ($_GET["order"]),
);
然后您可以在您的链接中使用 URLs,例如:
<a href="'.get_site_url().'/artists-alphabetical?orderby=title&order=ASC" >ALPHABETICAL</a>
$_Get
从 URL 接收一个变量并将其传递到您的查询中。