I'm building a wordpress theme and I need to check if there are any Post written within certain Post Format.
This is what I have in my functions.php
add_theme_support( 'post-formats', array( 'image', 'link', 'quote', 'status', 'video', 'audio', 'gallery' ) );
And this little piece of code in page template file.
if ( current_theme_supports( 'post-formats' ) ){
$post_formats = get_theme_support( 'post-formats' );
if ( is_array( $post_formats[0] ) ) {
foreach ($post_formats[0] as $post_format) {
echo ''.$post_format.'';
}
}
}
So, currently I have ALL Post Formats displayed as links. What I need is to display ONLY the ones that have Posts with that Post Format assigned.
Example:
If there are no Posts with Post Format Quote assigned, don't display quote link.
I've tried to search the web but without success. Does anyone know how to accomplish this? Thank you!
$terms = get_terms("post_format");
$count = count($terms);
if ( $count > 0 ){
echo '<ul class="list-group">';
foreach ( $terms as $term ) {
if($term->count > 0)
echo ''.$term->name.'';
}
echo '</ul>';
}
Try This
Related
as the title said, I'm trying to write a custom WordPress filter which limits the creation of Tag pages so that Tag pages are only created if the Tag has more than 10 associated posts. This is because we have so many tags with <10 associated posts, and it's creating a lot of noise.
I've not worked with WordPress for almost 5 years now, so I'm a bit rusty.
Here's what I'm trying, and it's not quite working:
<?php
function limit_taxonomies_by_count( $args, $taxonomy, $object_type ) {
$terms = get_terms('post_tag');
foreach($term in $terms) {
if ($term->count < 10) {
$args = array(
'public' => false
)
}
}
return $args
}
add_filter('register_taxonomy_args', 'limit_taxonomies_by_count' );
?>
Please let me know what I'm missing!
You can do as follows to achieve your job done. You can remove those tags link whose associated posts count are less than 10, so visitors can never click on those tags.
function modify_term_link_url( $links ) {
global $post;
if( !$post ) return $links;
$terms = get_the_terms( $post->ID, 'post_tag' );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( empty( $terms ) ) {
return false;
}
$links = array();
foreach ( $terms as $term ) {
if( $term->count < 10 ){
$link = '';
}else{
$link = get_term_link( $term, 'post_tag' );
if ( is_wp_error( $link ) ) {
return $link;
}
}
$links[] = '' . $term->name . '';
}
return $links;
}
add_filter( 'term_links-post_tag', 'modify_term_link_url' );
Codes goes to your active theme's functions.php
Instead of preventing the admins / editors from adding new tags, you could just "hide" Tag archive pages that don't meet the criteria (10 or more posts assigned to them). This way, admins/editors can still create / use new tags that might eventually reach 10 or more posts which will then make them visible to visitors.
To do so, you can use the template_redirect action hook to do something before the Tag archive page is loaded on screen (that something is explained next), then the is_tag() function to check whether the visitor is trying to access a Tag archive page, and finally the wp_redirect() function to do the actual redirection:
/**
* Redirects visitors to the homepage for Tags with
* less than 10 posts associated to them.
*/
function wp76515_tag_maybe_redirect(){
// We're viewing a Tag archive page
if ( is_tag() ) {
// Get Tag object
$tag = get_tag(get_queried_object_id());
// Tag's post count
$post_count = $tag->count;
// This tag has less than 10 posts,
// redirect visitor
if ( $post_count < 10 ) {
wp_redirect(
home_url(), // The URL we're sending the visitor to
'302' // The HTTP status, 302 = 'Moved Temporarily'
);
}
}
}
add_action('template_redirect', 'wp76515_tag_maybe_redirect', 5);
You may want to change the redirect code to 301 (Moved Permanently) to remove existing Tag pages with less than 10 posts from Google's index as well.
Looking to exclude specific category (ID = 100) from "Related Posts" feed at bottom of Blog pages. Extra bonus if it can also exclude from the sidebar archive (not sure if they are connected??)
I'm using WP Theme "TheFox", have asked them - not part of their theme.
I "think" it has to do in the functions.php. I have found some similar questions, and code, but have not had any luck.
I'm a complete noob for .php, so be gentle :)
I've found some other attempts, no luck. Not registering or effecting the feed.
$categories_to_exclude [ 100 ];
$first_cat = false;
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
array_shift($categories);
}
else {
$first_cat = $categories[0]->cat_ID;
}
}
What I could gather from your question is that You want to ignore one category (may be more) in the related post query?
use the following CODE (some explanation is given within the CODE in comments):
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 4,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
// reset $post after custom loop ends (if you need the main loop after this point)
wp_reset_postdata();
Use the below code, it must work
$categories_to_exclude [ 81, 11, 21 ];
$first_cat = false;
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
array_shift($categories);
}
else {
$first_cat = $categories[0]->cat_ID;
}
}
You get the categories with get_the_category. Then in the while loop you skip the first category if it's 81, and look again. If it's not 81 (and you still have categories available), you asign it to $first_cat and carry on.
I'm new to WordPress (duh) and trying something I'm very close to getting.
I have several Sub-Pages, each of which has attachments - images. I simply want to display these images. The client will regularly be adding more subpages so the code has to know to only display the images of its self, so to speak. An issue I'm running into is each subpage is displaying all the attachments of all subpages.
In my Functions.php I have this code to determine if the page is infact a subpage:
//find out if it is a suppage to use in page.php as function 'is_subpage'
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
return $post->post_parent; // return the ID of the parent post
} else { // there is no parent so ...
return false; // ... the answer to the question is false
}
}
In my page.php I have an If statement, for all my usual pages. It culminates in this:
elseif ( is_subpage() ) {
get_template_part( 'exhibition-template' );
get_template_part( 'normalfooter' );
}
In exhibition-template.php, where I want my images to appear I have this:
<?php
if ( is_subpage( $post->ID ) ) { //I think I screwed up here, innermost brackets
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID, //or is my issue here...?
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, 'large', false, false);
}
}
}
?>
However, what happens is each and every subpage simply displays all the images across all subpages, and frankly, I'm just too DUMB to know how to progress, or what to play with, where to start.
Any ideas?
It looks like you have a few issues.
When testing to see if a page is a subpage you should test to see if $post->post_parent is greater than 0. So like this:
if ( is_page() && $post->post_parent > 0 ) {
return $post->post_parent;
} else {
return false;
}
You do not need to pass any arguments into if ( is_subpage( $post->ID ) )... You could also just use WordPress' get_attached_media() function to get these attachements as well. This is how your exhibition-template.php could look:
if ( is_subpage() ) {
$attachments = get_attached_media('image', $post->ID);
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, 'large', false, false);
}
}
I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend.
Normally this code should ork but no idea what happend to the code
Please Help.
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';
return $choices;
}
add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $choices )
{
$args = array(
'hierarchical' => true,
'_builtin' => false
);
$posttypes = get_post_types( $args );
if( $posttypes )
{
foreach( $posttypes as $posttype ):
if( $posttype != 'acf' ):
$args = array(
'post_type' => 'designer',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$customposts = get_posts( $args );
if ( $customposts ) {
foreach( $customposts as $custompost ){
$choices[ $custompost->ID] = $custompost->post_title;
}
}
endif;
endforeach;
}
return $choices;
}
//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent', 'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
global $post;
$selected_post = (int) $rule['value'];
// post parent
$post_parent = $post->post_parent;
if( $options['page_parent'] ) {
$post_parent = $options['page_parent'];
}
if ($rule['operator'] == "=="){
$match = ( $post_parent == $selected_post );
}
elseif ($rule['operator'] != "!="){
$match = ( $post_parent != $selected_post );
}
return $match;
}
Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.
I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.
Ok sorry I understand the issue now and I have recreated the issue on my local install.
On the line of code below you are looking for the post_parent but I think you should be looking for the ID.
I changed this:
$post_parent = $post->post_parent;
to this:
$post_parent = $post->ID;
and it's working for me.
If I understand your problem correctly, in wp-admin post edit page click on screen options on the upper right corner. In the menu that appears make sure the Custom fields is selected. This will make the custom fields appear for edit.
What I want to do
I have a website that uses a CMS I wrote some time ago, and now I am trying to migrate it to wordpress.
At the existing implementation, when someone writes a post, they can add some extra images that are shown as a gallery at the end of the post, as you can see in this page for example (sorry for non english page): http://apollonpatras.gr/news/562/i-bradia-ton-xorigon-parousiasi-xorigikou-programmatos-kai-eisitirion-diarkeias/.
How I think I can do it
I am thinking about letting the users create wordpress galleries and at post save time intercept the post contents and store the gallery image ids in a postmeta field so I can show them however I want.
Also, I will have to strip the galleries from the content before they are shown, since I will show them in my own way later.
What I am trying so far
add_filter('content_save_pre', 'intercept_galleries', 99);
function intercept_galleries($content) {
if (get_post_type() !== 'post') {
return $content;
}
if (has_shortcode($content, 'gallery')) {
// The [gallery] short code exists.
$a = get_post_gallery(0, false);
update_post_meta(get_the_ID(), 'has_gallery', 1);
update_post_meta(get_the_ID(), 'gallery_items', $a['ids']);
} else {
update_post_meta(get_the_ID(), 'has_gallery', 0);
update_post_meta(get_the_ID(), 'gallery_items', "");
}
return $content;
}
add_filter('the_content', 'remove_shortcodes_from_content');
function remove_shortcodes_from_content($content) {
return strip_shortcodes($content);
}
Where it goes wrong
Looks like when the post is originally saved, the postmeta field "has_gallery" is set to 1, but the field "gallery_items" is empty.
When I go to the wordpress editor and just hit update, the fields are absolutely correct.
Also the hook to remove shortcodes from the content is working.
What am I looking for
How can I fix this problem? Also, is there something wrong/stupid with the way I decided to do this? Would some other way be cleaner/easier/faster etc?
Thank you for your time
I've done this a few times and here's how I do it.
First I create a function that will display the gallery in the way that I want. You can modify this according to how you require you gallery markup to be:
function my_gallery_shortcode( $attr ) {
$post = get_post();
if ( ! empty( $attr['ids'] ) ) {
$attr['include'] = $attr['ids'];
}
extract( shortcode_atts( array(
'order' => 'ASC',
'orderby' => 'post__in',
'id' => $post->ID,
'columns' => 3,
'size' => 'large',
'include' => '',
), $attr));
$id = (int) $id;
$columns = (int) $columns;
if ( 'RAND' == $order ) {
$orderby = 'none';
}
if ( ! empty( $include ) ) {
$_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if ( empty( $attachments ) ) {
return '';
}
$output = '<div class="slideshow"><ul>';
foreach ( $attachments as $id => $attachment ) {
$thumb = wp_get_attachment_image_src( $id, 'large', false );
$output .= '<li><img src="' . $thumb[0] . '" width="' . $thumb[1] . '" height="' . $thumb[2] . '" alt="' . get_post_meta( $id, '_wp_attachment_image_alt', true ) . '" /></li>';
}
$output .= '</ul></div>';
return $output;
}
You can complicate or simply the function above according to your requirements.
Then in your theme's functions.php add this:
add_shortcode( 'gallery', 'my_gallery_shortcode' );
Now you have two choices:
1) You can allow your user to add a gallery to the main page content by way of them editing the page in question and going to Media > Create Gallery
This will insert the gallery short code which will be formatted according to your function my_gallery_shortcode(), however the gallery can be managed via WordPress's gallery functionality in the admin area and is stored in the database by WordPress in the conventional way.
or
2) You could create a separate WYSIWYG field either through additional code in your functions.php file, or by using a plugin such as Advanced Custom Fields. You would then use this additional WYSIWYG field to allow the user to insert the gallery shortcode in the same way as above. This is virtually the same as option 1 above but you'd have more flexibility as to where you output and position the gallery on the page.
I hope this helps anyone looking to do the same thing.