Printing links individually in Worppress - php

I basically want to create a category in the "Links" section of wordpress and add a number of links and titles for that category. Simple stuff.
I then want to be able to, in my template file, echo the links and title or anything about the link individually as i please. Preferably in a loop as I have some page building to do before and after the links.
I know 'category_before' and 'category_after' exist but they won't do what I need.
So I tried,
<?php $args = array(
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
'category' => '3',
'hide_invisible' => 1,
'show_updated' => 0,
'echo' => 1,
'categorize' => 0,
'category_orderby' => 'name',
'category_order' => 'ASC',
'class' => 'linkcat',
'category_before' => '<tr><td>',
'category_after' => '</td></tr>' );
wp_list_bookmarks( $args );
?>
But that does a few things wrong. I don't need the category title or anything else besides the link text and destination really.
I am hoping to have a 'for' loop that will loop all links and I can just build my code section and links inside that, but let me know if there is a better way.
Thanks
EDIT: More Info
So I tried:
<?php
$taxonomy = 'link_category'; // Taken from the DB table
$tax_terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?></ul>
Which is the closest I have got. This only returns the info about the category not what is in the category.
There is nothing in the "wp_term_taxonomy" table in the DB about the actually category I have made.
Thanks again
EDIT:
Here is the area I'm referring to:
I want to show these 2 links

You may want to try the get_bookmarks function.
$bookmarks = get_bookmarks( array(
'orderby' => 'name',
'order' => 'ASC',
'category_name' => 'category-name'
));
// Loop through each bookmark and print formatted output
foreach ( $bookmarks as $bookmark ) {
printf( '<a class="relatedlink" href="%s">%s</a><br />', $bookmark->link_url, $bookmark->link_name );
}
https://codex.wordpress.org/Function_Reference/get_bookmarks

For more control you can use the function get_terms :
<?php
//list terms in a given taxonomy
$taxonomy = 'category'; // Pass default category or any custom taxonomy name
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
For info functions & its parameter:
https://developer.wordpress.org/reference/functions/get_terms/

Related

How to get_terms $count for custom-taxonomy

Have a custom-taxonomy called campaign_action with three categories called draft, live and paused.
I would like to display just the count for each but not in a list.
For example, I would like to echo the count for each individually as -
<li>Draft (<?php //code to display a number count for drafts ?>)</li>
<li>Live (<?php //code to display a number count for live ?>)</li>
<li>Paused (<?php //code to display a number count for paused ?>)</li>
I have successfully done this by displaying
foreach ( $terms as $term ) {
echo '(' . $term->count . ')';
}
However, this does not work for me and I want to get the $count for each individually.
Thank you for your help.
EDIT
To show further what I have in place currently
<?php
$terms = get_terms('campaign_action');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo '(0)';
foreach ( $terms as $term ) {
echo '(' . $term->count . ')';
}
}
?>
This will show all counts for each individual category but I only want to show the count for the category of draft within the custom_taxonomy of campaign_action
Here is an image of what the above achieves when added to the end of the Drafts. I want it to only show the count for the category of drafts within the custom-taxonomy of campaign_action. If it has zero posts with that category then it should show zero.
Try below code and read my comments of code.
echo wp_list_categories( array(
'orderby' => 'name',
'show_count' => true,
'taxonomy' => 'campaign_action' //i guess campaign_action is your taxonomy
) );
There is second way as well for custom html layout please check below code for custom html layout
$terms = get_terms(array(
'taxonomy' => 'campaign_action',//i guess campaign_action is your taxonomy
'hide_empty' => false
));
echo $terms->name;
echo $terms->count;
After Your question is edited :
$terms = get_terms(array(
'taxonomy' => 'campaign_action',//i guess campaign_action is your taxonomy
'hide_empty' => false
));
foreach ($terms as $terms)
{
if($terms->name == 'Draft')
{
echo $terms->name;
echo $terms->count;
}
}
You need to some arguments :
<?php
$args = array(
'post_type' => 'campaign_action',
'post_status' => 'publish' //(Or Draft...etc)
);
$show_recipes= get_posts( $args );
echo $show_recipes->post_count;
?>
Here is the full list of statuses in WP : https://codex.wordpress.org/Post_Status

Tag list filter for posts

I am trying to add a filter based on tags but im not sure how to proceed I have the tags being displayed in my post list with their anchor tags.
<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
echo '' . $tag->name . ' (' . $tag->count . ') ';
}
?></p>
I am using the following for my loop to get my posts
<?php
$myposts = get_posts('numberposts=-1&offset=$debut');
foreach ($myposts as $post):
setup_postdata($post);
?>
So My Questions
1 How do i go about adjusting my get_posts to apply a filter based on tag selection. Would this be easy to do with ajax
2 What is best way to generate the tag link list so that the above can be performed.
Edit
What im wanting to do is make sure I just get the url slug from tag/tag-3 how do i get this.
Edit
Ok i have made it a bit futher but its still only showing all posts in my tag page even though the single tag title is not blank so what gives ?.
<div class="post-list" style="width:80%;float:left">
<?php
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$myposts = get_posts($args);
foreach ($myposts as $post):
setup_postdata($post);
?>
<div id="dateInfo" style="float:right;">
<?php the_date('Y.m.j'); ?> |
<?php comments_number( '0 hozzászólás', '1 hozzászólás', '% hozzászólás' ); ?>.
</div>
<div id="title_wrapper">
<h2><?php the_title(); ?></h2>
</div>
Try this put your taxonomy and term name here
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'jazz'
)
)
);
$postslist = get_posts( $args );
if you want this done by ajax call then you will have to run an onclick function on <a> tags and call the above code in ajax response
this will return you with the tag filtered posts..
Need to pass taxonomy and terms by onclick function
Ok so as you have list of <a> tags, modify it to :
Get you taxonomy name and term ID is already coming here. it will be
passed in onclick function.
<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
$term_ID = $tag->term_id;
$taxonomy_name = 'GET TAXONOMY NAME HERE';
echo '<a onclick="show_filter_tags('.$term_ID.','.$taxonomy_name.')" href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' (' . $tag->count . ') </a>';
}
?>
define the onclick function in the <a> tag
<script type="text/javascript">
function show_filter_tags(term_id,taxonomy_name){
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
$.post(
ajaxurl,
{
'action' : 'get_tagged_posts',
'term_id' : term_id,
'taxonomy_name' : taxonomy_name,
},
function(output){
console.log(output)
});
}
</script>
Now here is the function where query needs to be written which will filter the posts on basis of tags. Pu this in functions.php file in active theme folder
add_action('wp_ajax_get_tagged_posts', 'get_tagged_posts');
add_action('wp_ajax_nopriv_get_tagged_posts', 'get_tagged_posts');
function get_tagged_posts() {
$args = array(
'tax_query' => array(
array(
'taxonomy' => $_REQUEST['taxonomy_name'],
'field' => 'term_id',
'terms' => $_REQUEST['term_id'],
'operator' => 'IN',
)
)
);
$postslists = get_posts( $args );
foreach ($postslists as $postslist) {
# code...
}
}

wordpress get_categories count also custom post type

on my home page i want to present how many posts are for each category. so i use get_categories->count for that. but i have a news custom post type. each news item is associated with a category. so how can i exclude the custom post type from the count?
here is the code i have:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => array(1,8),
'include' => '',
'number' => '',
'taxonomy' => array('category'),
'pad_counts' => true
);
$categories = get_categories( $args );
then i use within the loop: echo $category->count
If I understood your question correctly, this should help you out. The following code will list all of your post categories with their respective post counts, omitting the News CPT posts from the count total.
<?php $cats = get_categories(); // Get categories ?>
<?php if ($cats) : ?>
<ul>
<?php // Loop through categories to print name and count excluding CPTs ?>
<?php foreach ($cats as $cat) {
// Create a query for the category to determine the number of posts
$category_id= $cat->term_id;
$cat_query = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => -1,
'cat' => $category_id
) );
$count = $cat_query->found_posts;
// Print each category with it's count as a list item
echo "<li>" . $cat->name . " (" . $count . ")</li>";
} ?>
<?php wp_reset_query(); // Restore global post data ?>
</ul>
<?php endif; ?>

WordPress: Get all tags from custom post type

I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
}

WORDPRESS Simple Query

I have a category query, and in my category query I want to get product (only one) by queried category id (or name or whatsoever)
I start query:
<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?>
and then try to use get_posts() function to get product:
$args = array(
'post_type' => 'wpsc-product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'wpsc_product_category',
'field' => 'id',
'terms' => $aka
)));
$cat1_posts = get_posts($args);
where $aka is:
$aka = '[wpsc_category_id]';
but when I echo $cat1_posts[0]->ID; it only shows my last product ID for every category. what is the problem? echoing only [wpsc_category_id] works perfect.
I tried EVERYTHING for the last few days. I will buy you cookies for help
I've got to idea, that I need foreach or anything like this
You can use the get_terms() function. So something like this (untested)
<?php
//for each category, show latest post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms( 'wpsc_product_category');
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'post_type' => 'wpsc-product',
'wpsc_product_category' => array($category->slug)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>

Categories