如何在 PHP (wordpress) 中创建可过滤的投资组合?
How can I create a filterable portfolio in PHP (wordpress)?
我目前正在开发自己的个人作品集网站。我使用 Pods 创建了自定义 post 类型来创建投资组合项目,我想使用 wordpress 标签作为过滤器。
我已成功收到投资组合项目和过滤器。但是我怎样才能使它们 clickable/filterable 呢?我发现的所有内容都包括投资组合插件等。但我不想使用它,只想创建我自己的简单可过滤投资组合。
有人可以帮我吗?
代码如下:
过滤器:
$tags = get_tags();
$html = '<div class="post_tags centered-content"> <a href="" class="button" title="Alle projecten">Alles</a>';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='#{$tag->slug}' title='{$tag->name} filter' class='button outline {$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
投资组合项目:
function getPortfolio(){
global $post;
$portfolioargs = array(
'posts_per_page' => 999,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'portfolio',
'post_status' => 'publish',
'suppress_filters' => false
);
$portfolioitems = get_posts($portfolioargs);
foreach ($portfolioitems as $portfolioitem) {
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($portfolioitem->ID), 'full' );
$tags = wp_get_post_tags($portfolioitem->ID);
echo '<div class="card portfolio">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'">';
echo '<figure>';
echo '<img src="'. pods_image_url($feat_image, 'card') .'"/>';
echo '</figure>';
echo '</a>';
echo '<div class="card-title-wrapper">';
echo '<h3>'. $portfolioitem->post_title .'</h3>';
echo '<span class="card-subtitle mdi mdi-tag-outline mdi-15px">';
foreach ( $tags as $tag ) {
echo $tag->name . ', ';
}
echo '</span>';
echo '</div>';
echo '<div class="card-content">';
echo '<p>'. $portfolioitem->post_content .'</p>';
echo '</div>';
echo '<div class="card-actions">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'" class="button flat">Lees meer</a>';
echo '<a href="'. $portfolioitem->website_url .'" class="button flat" target="_blank">Bekijk website</a>';
echo '</div>';
echo '</div>';
}
}
Check the screenshot here
在我看来,让这些标签充当过滤器的最佳方式是 "AJAX"。
我在这里编写了您的 "tags" 代码作为 javascript 过滤器。希望能帮助到你。
首先让我们重写您的代码以显示标签,我添加一行(创建一个输入以使用 AJAX 发送):
$tags = get_tags();
$html = '<div class="post_tags centered-content">
<input type='hidden' name='tag_filter' value=''>
<a href="" class="button" title="Alle projecten">Alles</a>';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a title='{$tag->name} filter' class='button outline filterBtn {$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
然后我们将让我们的 javascript (jquery) 发送选定的标签值:
$('.filterBtn').click(function(){
var selected_tag = $(this).text();
$('input[name="tag_filter"]').val(selected_tag);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', filter: $('input[name="tag_filter"]').val()},
success: function (data) {
$('#yourResultDIV').html(data);
}
});
})
下一部分是我们的 PHP 代码,用于根据我们选择的过滤器生成结果:(在 functions.php 中)
add_action('wp_ajax_data_fetch' , 'resultsLoad');
add_action('wp_ajax_nopriv_data_fetch','resultsLoad');
function resultsLoad(){
$filter = $_POST['filter'];
global $post;
$portfolioargs = array(
'posts_per_page' => 999,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'portfolio',
'post_status' => 'publish',
'tag' => $filter
);
$portfolioitems = get_posts($portfolioargs);
foreach ($portfolioitems as $portfolioitem) {
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($portfolioitem->ID), 'full' );
$tags = wp_get_post_tags($portfolioitem->ID);
echo '<div class="card portfolio">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'">';
echo '<figure>';
echo '<img src="'. pods_image_url($feat_image, 'card') .'"/>';
echo '</figure>';
echo '</a>';
echo '<div class="card-title-wrapper">';
echo '<h3>'. $portfolioitem->post_title .'</h3>';
echo '<span class="card-subtitle mdi mdi-tag-outline mdi-15px">';
foreach ( $tags as $tag ) {
echo $tag->name . ', ';
}
echo '</span>';
echo '</div>';
echo '<div class="card-content">';
echo '<p>'. $portfolioitem->post_content .'</p>';
echo '</div>';
echo '<div class="card-actions">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'" class="button flat">Lees meer</a>';
echo '<a href="'. $portfolioitem->website_url .'" class="button flat" target="_blank">Bekijk website</a>';
echo '</div>';
echo '</div>';
}
die();
}
我目前正在开发自己的个人作品集网站。我使用 Pods 创建了自定义 post 类型来创建投资组合项目,我想使用 wordpress 标签作为过滤器。
我已成功收到投资组合项目和过滤器。但是我怎样才能使它们 clickable/filterable 呢?我发现的所有内容都包括投资组合插件等。但我不想使用它,只想创建我自己的简单可过滤投资组合。
有人可以帮我吗?
代码如下:
过滤器:
$tags = get_tags();
$html = '<div class="post_tags centered-content"> <a href="" class="button" title="Alle projecten">Alles</a>';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='#{$tag->slug}' title='{$tag->name} filter' class='button outline {$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
投资组合项目:
function getPortfolio(){
global $post;
$portfolioargs = array(
'posts_per_page' => 999,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'portfolio',
'post_status' => 'publish',
'suppress_filters' => false
);
$portfolioitems = get_posts($portfolioargs);
foreach ($portfolioitems as $portfolioitem) {
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($portfolioitem->ID), 'full' );
$tags = wp_get_post_tags($portfolioitem->ID);
echo '<div class="card portfolio">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'">';
echo '<figure>';
echo '<img src="'. pods_image_url($feat_image, 'card') .'"/>';
echo '</figure>';
echo '</a>';
echo '<div class="card-title-wrapper">';
echo '<h3>'. $portfolioitem->post_title .'</h3>';
echo '<span class="card-subtitle mdi mdi-tag-outline mdi-15px">';
foreach ( $tags as $tag ) {
echo $tag->name . ', ';
}
echo '</span>';
echo '</div>';
echo '<div class="card-content">';
echo '<p>'. $portfolioitem->post_content .'</p>';
echo '</div>';
echo '<div class="card-actions">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'" class="button flat">Lees meer</a>';
echo '<a href="'. $portfolioitem->website_url .'" class="button flat" target="_blank">Bekijk website</a>';
echo '</div>';
echo '</div>';
}
}
Check the screenshot here
在我看来,让这些标签充当过滤器的最佳方式是 "AJAX"。 我在这里编写了您的 "tags" 代码作为 javascript 过滤器。希望能帮助到你。 首先让我们重写您的代码以显示标签,我添加一行(创建一个输入以使用 AJAX 发送):
$tags = get_tags();
$html = '<div class="post_tags centered-content">
<input type='hidden' name='tag_filter' value=''>
<a href="" class="button" title="Alle projecten">Alles</a>';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a title='{$tag->name} filter' class='button outline filterBtn {$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html;
然后我们将让我们的 javascript (jquery) 发送选定的标签值:
$('.filterBtn').click(function(){
var selected_tag = $(this).text();
$('input[name="tag_filter"]').val(selected_tag);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', filter: $('input[name="tag_filter"]').val()},
success: function (data) {
$('#yourResultDIV').html(data);
}
});
})
下一部分是我们的 PHP 代码,用于根据我们选择的过滤器生成结果:(在 functions.php 中)
add_action('wp_ajax_data_fetch' , 'resultsLoad');
add_action('wp_ajax_nopriv_data_fetch','resultsLoad');
function resultsLoad(){
$filter = $_POST['filter'];
global $post;
$portfolioargs = array(
'posts_per_page' => 999,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'portfolio',
'post_status' => 'publish',
'tag' => $filter
);
$portfolioitems = get_posts($portfolioargs);
foreach ($portfolioitems as $portfolioitem) {
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($portfolioitem->ID), 'full' );
$tags = wp_get_post_tags($portfolioitem->ID);
echo '<div class="card portfolio">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'">';
echo '<figure>';
echo '<img src="'. pods_image_url($feat_image, 'card') .'"/>';
echo '</figure>';
echo '</a>';
echo '<div class="card-title-wrapper">';
echo '<h3>'. $portfolioitem->post_title .'</h3>';
echo '<span class="card-subtitle mdi mdi-tag-outline mdi-15px">';
foreach ( $tags as $tag ) {
echo $tag->name . ', ';
}
echo '</span>';
echo '</div>';
echo '<div class="card-content">';
echo '<p>'. $portfolioitem->post_content .'</p>';
echo '</div>';
echo '<div class="card-actions">';
echo '<a href="'. get_the_permalink($portfolioitem->ID) .'" class="button flat">Lees meer</a>';
echo '<a href="'. $portfolioitem->website_url .'" class="button flat" target="_blank">Bekijk website</a>';
echo '</div>';
echo '</div>';
}
die();
}