Skip to content

Instantly share code, notes, and snippets.

@FreshLondon
Created August 30, 2022 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreshLondon/5400590a3e8a5bd48416b34fa4619146 to your computer and use it in GitHub Desktop.
Save FreshLondon/5400590a3e8a5bd48416b34fa4619146 to your computer and use it in GitHub Desktop.
<?php
function fl_archive_cats() {
$args = array( 'taxonomy' => 'katagori' );
$categories = get_categories( $args );
?>
<div class="fl-archive-cats">
<?php
foreach ( $categories as $category ) {
$term = 'term_' . $category->term_id;
$deactivate = get_field( 'deactivate', $term );
if ( ! $deactivate ) { ?>
<a href="<?= get_category_link( $category->term_id ); ?>" class="post-cat-button">
<?= $category->name; ?>
</a>
<?php
}
} ?>
</div>
<?php
}
function fl_archive_cats_top() {
if ( is_tax() ) {
$term = get_queried_object();
$long_description = get_field( 'long_description', $term );
if ( $long_description ) {
?>
<div id="archive-cats-top">
<h1 id="act-title"><?= $term->name; ?></h1>
<div id="act-description"><?= $long_description; ?></div>
</div>
<?php
}
}
}
function fl_archive_search() { ?>
<form role="search" method="get" id="searchform" class="searchform" action="<?= home_url( '/' ); ?>">
<label class="screen-reader-text" for="s">Eller skriv søgeord:</label>
<input type="text" name="s" id="s" placeholder="f.eks. tomater"/>
<input type="hidden" name="post_type" value="opskrifter"/>
<input type="submit" id="searchsubmit" class="button" value="SØG"/>
</form>
<?php
}
// adding filters for admin ajax
add_action( 'wp_ajax_postfilter', 'fl_post_filter_function' );
// wp_ajax_{ACTION HERE}
add_action( 'wp_ajax_nopriv_postfilter', 'fl_post_filter_function' );
//
function fl_post_filter_function() {
$args = array(
'post_type' => 'opskrifter',
'orderby' => 'date', // we will sort posts by date
'order' => 'ASC', // ASC or DESC
);
$args['tax_query'] = array();
if ( isset( $_POST['category_filter'] ) ) {
$category_filter = $_POST['category_filter'];
$args['tax_query'] = array(
array(
'taxonomy' => 'katagori',
'field' => 'id',
'terms' => $category_filter,
)
);
}
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<div class="oxy-easy-posts oxy-posts-grid">
<div class="oxy-posts">
<?php while ( $query->have_posts() ): $query->the_post();
// do post
fl_post_archive_single_mini();
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif; ?>
</div>
</div>
<?php
die();
}
/// post archive and post search results
function fl_post_archive( $page = 1, $post_type = 'post', $cat_slug = null, $search = null ) {
global $post;
$s = $search ?? get_search_query();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : $page;
$cat_req = $_SERVER['REQUEST_URI'];
$args = array(
'post_type' => $post_type,
'orderby' => 'date', // we will sort posts by date
'order' => 'DESC', // ASC or DESC
'paged' => $paged,
);
if ( $post_type == 'opskrifter' ) {
if ( strpos( $cat_req, 'katagori' ) ) {
$cat_start = ( strpos( $_SERVER['REQUEST_URI'], 'katagori' ) + 9 );
$cat_name = substr( $cat_req, $cat_start, - 1 );
$category = get_term_by( 'slug', $cat_name, 'katagori' );
$category_id = $category->term_id;
} elseif ( array_key_exists( 'cat_slug', $_POST ) ) {
(int) $category_id = $_POST['cat_slug'];
$cat_name = $_POST['cat_slug'];
}
if ( isset( $category_id ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'katagori',
'field' => 'slug',
'terms' => $cat_name,
)
);
}
}
if ( $s != null ) {
$args['s'] = $s;
}
$query = new WP_Query( $args );
$skipWrapper = false;
if ( $paged > 1 ) {
$skipWrapper = true;
goto skipWrapper;
}
if ( $query->have_posts() ) :
if ( $post_type == 'post' ) {
$post_count = $query->post_count;
$loop_count = 1;
}
?>
<div class="oxy-easy-posts oxy-posts-grid">
<div class="oxy-posts" id="oxy-posts">
<?php
skipWrapper:
while ( $query->have_posts() ): $query->the_post();
fl_post_archive_single_mini();
if ( $post_type == 'post' ) {
if ( ( $post_count < 4 ) and ( $loop_count == $post_count ) ) { ?>
<div class="posts-coming oxy-post">
<div class="oxy-post-image-fixed-ratio placeholder"></div>
<div class="oxy-post-wrap">
<h2 class="oxy-post-title">Ny hjemmeside – flere indlæg følger…</h2>
</div>
</div>
<?php
}
$loop_count ++;
}
endwhile;
if ( $skipWrapper ) {
return;
}
$total_pages = $query->max_num_pages;
if ( $total_pages > 1 ) {
$current_page = max( 1, get_query_var( 'paged' ) ); ?>
<?php }
wp_reset_postdata();
else :
echo 'No posts found';
endif; ?>
</div>
</div>
<div class="archive-pagination">
<button type="button" id="loadMore" class="button button-outline-white">VIS FLERE</button>
</div>
<script>
var page = 1;
var maxPage = <?= $query->max_num_pages; ?>;
if (page >= maxPage) {
jQuery('#loadMore').hide();
}
jQuery('#loadMore').on('click', function () {
page++;
jQuery.post({
url: '/wp-content/plugins/FreshLondon-toolkit/functions/post-archive-load-more-posts.php',
// contentType: 'application/json',
data: {
post_type: '<?=$post_type;?>',
page: page
<?= array_key_exists( 'tax_query', $args ) ? ', cat_slug: \'' . $args['tax_query'][0]['terms'] . '\'' : '' ?>
<?= $s ? ', search: \'' . $s . '\'' : '';?>
},
complete: function (Data) {
jQuery('#oxy-posts').html(jQuery('#oxy-posts').html() + Data.responseText)
if (page >= maxPage) {
jQuery('#loadMore').hide();
}
}
});
});
</script>
<?php
}
function fl_post_archive_single_mini() {
$post_type = get_post_type();
if ( $post_type == 'opskrifter' ) {
$image = get_field( 'images' )[0]['sizes']['archive-thumb'];
$alt = get_field( 'images' )[0]['alt'];
} else {
$image = get_the_post_thumbnail_url( get_the_ID(), 'archive-thumb' );
$alt = get_the_title();
}
?>
<div class='oxy-post' onclick="location.href='<?php the_permalink(); ?>';">
<a class='oxy-post-image' href='<?php the_permalink(); ?>'>
<div class='oxy-post-image-fixed-ratio'>
<img src="<?= $image; ?>" alt="<?= $alt; ?>">
</div>
</a>
<div class='oxy-post-wrap'>
<a class='oxy-post-title' href='<?php the_permalink(); ?>'>
<h2><?php the_title(); ?></h2>
</a>
</div>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment