如何在PHP(wordpress)中创建可过滤的投资组合?

问题描述 投票:0回答:1

我目前在自己的个人作品集网站上工作。我已经使用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

javascript php wordpress custom-post-type custom-taxonomy
1个回答
0
投票

我认为使这些标签用作过滤器的最佳方法是“ 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();
}
© www.soinside.com 2019 - 2024. All rights reserved.