Custom category template WP - wordpress

I have created a custom post type (activity) with category taxonomy , In the menu the user can select which category he want to display , the problem is that I created category.php but nothing appears on my site here is the code :
<h1><?php single_cat_title(); ?></h1>
<?php if (is_category('edition')) :
$args = array('category_name' => 'edition');
else :
$args = array('category_name' => 'diffusion');
endif; ?>
<?php $query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<p>' . get_the_title() . '</p>';
}
}
?>

I think you can call your file archive-activity.php to make it work .
For your custom post type it's archive-{post-type}.php, and keep the same code you've posted.
The archive-{post-type}.php template provides the most general form of control, providing a layout for custom post type archives, a page that displays a list of posts.
To use different template for your special taxonomy create a file call taxonomy-{taxonomy}-{term}.php or tag-{slug}.php to target specific term.
You can read more about the template hierarchy here : custom-post-type-template-files

Related

Woocommerce and Elementor template problem

I have web site WP + Woocommerce + Elementor. Im realise custom filter product by attribute values. But im not get normal template products item cells.
Im using this code, by ajax request:
$args = $this->filter_query_build(); // Create $args for WP_Query
$query = new WP_Query( $args );
$products='';
if ( $query->have_posts() )
{
while ( $query->have_posts() ) : $query->the_post();
ob_start();
wc_get_template_part( 'content', 'product' );
$products.= ob_get_contents(); // Get product item
ob_end_clean();
endwhile;
}
// Create JSON output
echo json_encode([
'products'=>$products,
]);
wp_die();
Result work this code is fine, i have list product.
But the html item product not equal html in category list or archive-product.
Product category and archive-product create by in Elementor.
Im need get template item cells product equal output on the product category and archive-product.
Tried to figure out where the elementor template is pulling from, but so far without success

how to show the data of CPT UI plugin on website?

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

shortcode passes custom post type name to pagetemplate

i have a page template in place assigned to a page named cartoonbooks. the custom post type is also called cartoonbooks. how do i get the page to pass the customposttype to the pagetemplate so that i can use the same template for different pages. i tried using shortcodes but unsuccessful ...plz help
functions.php
<?php
function my_shortcode_handler( $atts, $content = null ) {
extract( shortcode_atts( array(
'attr_1' => '',
// ...etc
), $atts ) );
return $attr_1;
}
add_shortcode( 'myshortcode', 'my_shortcode_handler' );
?>
page-books.php
<article>
<?php
//Define the loop based on arguments
$loop = new WP_Query( $attr_1);
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_content(); ?>
<?php endwhile;?>
</article>
in the page i put the short code
[myshortcode attr_1="cartoonbooks"]
i updated my code still it doesn't work ...heres the new one
functions.php
function shortcode_handler( $atts, $content = null ) {
extract( shortcode_atts( array(
'posttype' => '',
// ...etc
), $atts ) );
return do_shortcode($posttype);
}
function register_my_shortcode(){
add_shortcode( 'shortcode', 'shortcode_handler' );
}
add_action('init','register_shortcode');
?>
page.php
<?php
$args = do_shortcode($content);
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div><?php the_title('')?></div>
<?php endwhile;?>
in the page called carttonbooks itself i put the code
[myshortcode posttype="cartoonbooks"]
in anothe page called adventurebooks i put code
[myshortcode posttype="adventurebooks"]
but the template doesn't take the posttype from the shortcod and display he content...plzhelp
In wordpress for template file to call shortcode we usedo_shortcode()
try this:
<?php echo do_shortcode('[myshortcode "cartoonbooks"]'); ?>
Know more
Why do any of this. It seems to be a waste.
Check the post type in the template. http://codex.wordpress.org/Function_Reference/get_post_type
if(get_post_type( $post ) == 'cartoonbooks') {
//Your special post type template code
} else {
// Your normal template code
}
ok for those who may have this problem in future...i found the solution by test and trial method...and its very easy.
now say your posttype is books and the slug is books too so the archive page will be archive-books.
create a page with slug as books(slug os archive and page should be same) and add it to menu no need for any template it automatically grabs the archive-books content...so whatever styling u need put it on the archive page

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

How to display taxonomies instead of categories in my website home page by using WordPress (PHP)?

I have a taxonomy called operating_system.In the admin panel I have created a few taxonomy. On the homepage I want to display the taxonomies instead of categories. what can i do to get exact answer.please suggest me a suitable code.
How about this:
<?php
$args=array(
'name' => 'genre'
);
$output = 'objects'; // or objects
$taxonomies=get_taxonomies($args,$output);
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy->name . '</p>';
}
}
?>
You can see more details and examples in the codex.

Resources