I'm back struggling with some more coding :(
I have written a custom PHP page to loop through a custom post type. Now what I want is to list the custom posts categories, and have each category link to the same page but pass the category to the query_posts() function.
My code is currently
<div id="left" style="float:left;width:200px">
<b>Categories</b><br><br>
<?php wp_list_categories( $args ); ?>
</div> <!-- end of left container -->
<div id="middle" style="float:left;width:700px">
<?php
$args = array(
'post_type'=> 'e-books',
'showposts' => '-1',
'eCats' => $cat,
'order' => 'ASC'
);
query_posts( $args );
where
$cat = $_GET['cat']
What I want is each link from the wp_list_categories() to be something like.
http://example.com/products.php?cat=fiction
Is this possible? I can't get my head around the Walker class :( I've got this far
class MyWalker extends Walker_Category {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = '<a href="products.php?cat="';
$link .= $cat_name;
$link .= '>';
$link .= $cat_name . '</a>';
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
}
but the links aren't completing - they are showing as
?cat=
For listing category with the custom taxonomy :
You can you following code:
$catnames = get_terms( 'custom taxonomy name');
Then use foreach loop to disply the category.
Related
I'm trying to display an ACF field within a Wordpress loop using functions.php to create a shortcode. Unfortunately, the_field('conference_location') does not display.
function show_conferences_func() {
global $post;
$html = "";
$my_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 3,
'product_cat' => 'conferences'
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$html .= "<div class=\"conference-block\">";
$html .= "<h2>" . get_the_title() . " </h2>";
$hmtl .= "<p>" . the_field('conference_location') . "</p>";
$html .= "Learn more";
$html .= "</div>";
endwhile; endif;
return $html;
}
add_shortcode( 'show_conferences', 'show_conferences_func' );
You should use
get_field('conference_location')
instead of
the_field('conference_location')
the_field() echo your data. So you're not storing your value.
There are a handful of things I'd like to do with this shortcode I'm working on. My knowledge about this isn't the best, but I'm trying to learn.
/**
* Recent Project Shortcode
*/
function project_query() {
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$projects_query = new WP_Query( $args );
if ( $projects_query->have_posts() ) :
// var_dump(the_post_thumbnail_url("full")); exit;
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
while ( $projects_query->have_posts() ) :
$projects_query->the_post();
// Do stuff with each post here
$title = get_the_title();
$link = get_the_permalink();
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
endwhile;
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );
There are a few issues here. What does work is that on the front-end it pulls the project name, which is sweet, and the button links to the appropriate page.
Here's how I'd like the shortcode to look when using it: [show_projects posts_per_page="3" order="ASC"] I want to make it "easy" for the user to modify the $args. The second thing that isn't working is the background url I'm trying to do. Right now in the front end everything is outputting except that background url.
Hey Darren the problem is that you create the $featured_img variable after you use it. It should be in while loop.
Please try this code
/**
* Recent Project Shortcode
**/
function project_query($atts) {
$atts = shortcode_atts(
array(
'example_attribute' => 'example_value',
),
$atts, 'example'
);
//if you want to use the attribute you should use $atts['example_attribute'] for example
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) :
$post = array_shift( $posts );
$title = get_the_title($post->ID);
$link = get_the_permalink($post->ID);
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );
I'm making my first plugin and I have a problem with displaying my shortcode.
It shows in the top all the time but I have read some about ob_start(); and trying to use it but the shortcode just returns nothing.
Im using the code below - it seems to have something to do with my posts. Anyone know why how to solve this problem?
My WP_Query:
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
The code i use to display it:
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
The complete code for the shortcode function:
<?php
function cpt_content_func( $atts ) {
// Get the ID we putted in into [wh-postgrid id="THIS_ID"]
extract( shortcode_atts( array(
'id' => null
), $atts ) );
// Get stored meta data (For the categories - needed to be formatted in
// a certain way)
$categories = get_post_meta( $id, 'whpost_cats', true );
// Get meta data for settings and such.
$whpost_stored_meta_shortcode = get_post_meta( $id );
// Get the correct categories and use the settings we got.
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
// Set the styles
switch ( $whpost_stored_meta_shortcode['whpost_posts_per_line'][0] ) {
case 1:
$post_width = '100%';
$post_max_height = '';
break;
case 2:
$post_width = '50%';
$post_max_height = '';
break;
case 3:
$post_width = '33.333333%';
$post_max_height = '';
break;
case 4:
$post_width = '25%';
$post_max_height = '';
break;
default:
$post_width = '50%';
}
// Display the front-end
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
}
add_shortcode('wh-postgrid','cpt_content_func');
I think problem with your Query, you can add this line to check
if (!$query->have_posts()) {
return 'Empty result';
} else {
return ob_get_clean();
}
The function must return the output.
Change your code like this and it should work:
$output = '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$postId = get_the_ID();
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), 'large' );
$output .= '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
$output .= get_the_title( $postId );
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;
I have a related posts by author function using this code:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.
I'm working on a site that has custom author pages and on the author pages it has a recent author posts widget which displays other posts by that author. This site has multiple authors so it is different for every post and I haven't found a plugin that does this. I'm trying to display the post thumbnail, title, and category name.
I'm using a function that is displaying the title and the thumbnail, but it doesn't have the category . I tried to add the category in with: the_category(' ','multiple',$authors_post->ID) unfortunately it displays all of the categories in the first li. Instead of for each post.
Here is what I'm working with:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,
'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
. '<p>' . the_category(' ','multiple',$authors_post->ID)
. '</p> <a href="' . get_permalink( $authors_post->ID )
. '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Any help would be greatly appreciated,
Thanks!
Please try this code,
<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat)
{
echo '<h2>' . $yourcat->name . '</h2>';
}
?>
To get category name,
try some thing like
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$category = get_the_category($authors_post->ID);
foreach($category as $c){
$cat=$c->cat_name.' '.$cat;
}
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul>';
return $output;
}
hope this work for you..