【问题标题】:How can I create a filterable portfolio in PHP (wordpress)?如何在 PHP (wordpress) 中创建可过滤的投资组合?
【发布时间】:2019-11-25 10:57:13
【问题描述】:

我目前正在开发自己的个人作品集网站。我使用 Pods 创建了一个自定义帖子类型来创建投资组合项目,我想使用 wordpress 标签作为过滤器。

我已成功收到投资组合项目和过滤器。但我怎样才能让它们可点击/可过滤?我发现的所有东西都包括投资组合插件等。但我不想使用它,只想创建自己的简单可过滤投资组合。

有人可以帮帮我吗?

代码如下:

过滤器:

$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

【问题讨论】:

  • 所有这些代码的确切问题是什么?它有什么作用,什么还没有工作?
  • 嗯,问题是我不知道如何使过滤器真正起作用。我有一个过滤器按钮列表,但如果您明白我的意思,我不知道如何让这些按钮更改其下方的投资组合概览。
  • “嗯,问题是我不知道如何让过滤器真正起作用。” - 那么你应该首先做一些研究,然后尝试并弄清楚现有的解决方案是如何工作的。 (这不是请求私人教程的地方。)也许可以从css-tricks.com/… 之类的东西开始。

标签: javascript php wordpress custom-post-type custom-taxonomy


【解决方案1】:

在我看来,让这些标签充当过滤器的最佳方式是“AJAX”。 在这里,我已经编写了您的“标签”代码以用作 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();
}

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2015-05-19
    • 2017-05-15
    • 2018-01-23
    相关资源
    最近更新 更多